chamikaramj commented on a change in pull request #12841: URL: https://github.com/apache/beam/pull/12841#discussion_r491110612
########## File path: sdks/python/apache_beam/dataframe/io_test.py ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import glob +import os +import shutil +import sys +import tempfile +import unittest + +import apache_beam as beam +from apache_beam.dataframe import io + + +class IOTest(unittest.TestCase): + def setUp(self): + self._temp_roots = [] + + def tearDown(self): + for root in self._temp_roots: + shutil.rmtree(root) + + def temp_dir(self, files=None): + dir = tempfile.mkdtemp(prefix='beam-test') + self._temp_roots.append(dir) + if files: + for name, contents in files.items(): + with open(os.path.join(dir, name), 'w') as fout: + fout.write(contents) + return dir + os.sep + + def read_all_lines(self, pattern): + for path in glob.glob(pattern): + with open(path) as fin: + # TODO(Py3): yield from + for line in fin: + yield line.rstrip('\n') + + @unittest.skipIf(sys.version_info[0] < 3, 'unicode issues') + def test_write_csv(self): Review comment: test_read_csv (seems like this is testing read_csv) ? ########## File path: sdks/python/apache_beam/dataframe/io_test.py ########## @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import glob +import os +import shutil +import sys +import tempfile +import unittest + +import apache_beam as beam +from apache_beam.dataframe import io + + +class IOTest(unittest.TestCase): + def setUp(self): + self._temp_roots = [] + + def tearDown(self): + for root in self._temp_roots: + shutil.rmtree(root) + + def temp_dir(self, files=None): + dir = tempfile.mkdtemp(prefix='beam-test') + self._temp_roots.append(dir) + if files: + for name, contents in files.items(): + with open(os.path.join(dir, name), 'w') as fout: + fout.write(contents) + return dir + os.sep + + def read_all_lines(self, pattern): + for path in glob.glob(pattern): + with open(path) as fin: + # TODO(Py3): yield from + for line in fin: + yield line.rstrip('\n') + + @unittest.skipIf(sys.version_info[0] < 3, 'unicode issues') + def test_write_csv(self): + input = self.temp_dir({'1.csv': 'a,b\n1,2\n', '2.csv': 'a,b\n3,4\n'}) + output = self.temp_dir() + with beam.Pipeline() as p: Review comment: Add a test for write_cvs as well ? ########## File path: sdks/python/apache_beam/dataframe/io.py ########## @@ -0,0 +1,180 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +from io import BytesIO +from io import StringIO +from io import TextIOWrapper + +import pandas as pd + +import apache_beam as beam +from apache_beam import io +from apache_beam.dataframe import frame_base +from apache_beam.io import fileio + + +def read_csv(path, *args, **kwargs): + """Emulates `pd.read_csv` from Pandas, but as a Beam PTransform. + + Use this as + + df = p | beam.dataframe.io.read_csv(...) + + to get a deferred Beam dataframe representing the contents of the file. + """ + return _ReadFromPandas(pd.read_csv, path, args, kwargs) + + +def write_csv(df, path, *args, **kwargs): + from apache_beam.dataframe import convert Review comment: Import at top (here and below) ? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org