Skip to content

Commit 8c247c2

Browse files
committed
implement to read only columns from parquet file
1 parent 488db6f commit 8c247c2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pandas/io/parquet.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def write(self, df, path, compression='snappy',
7676
table, path, compression=compression,
7777
coerce_timestamps=coerce_timestamps, **kwargs)
7878

79-
def read(self, path):
79+
def read(self, path, columns=None):
8080
path, _, _ = get_filepath_or_buffer(path)
81-
return self.api.parquet.read_table(path).to_pandas()
81+
return self.api.parquet.read_table(path, columns).to_pandas()
8282

8383

8484
class FastParquetImpl(object):
@@ -115,9 +115,9 @@ def write(self, df, path, compression='snappy', **kwargs):
115115
self.api.write(path, df,
116116
compression=compression, **kwargs)
117117

118-
def read(self, path):
118+
def read(self, path, columns=None):
119119
path, _, _ = get_filepath_or_buffer(path)
120-
return self.api.ParquetFile(path).to_pandas()
120+
return self.api.ParquetFile(path).to_pandas(columns)
121121

122122

123123
def to_parquet(df, path, engine='auto', compression='snappy', **kwargs):
@@ -178,7 +178,7 @@ def to_parquet(df, path, engine='auto', compression='snappy', **kwargs):
178178
return impl.write(df, path, compression=compression)
179179

180180

181-
def read_parquet(path, engine='auto', **kwargs):
181+
def read_parquet(path, engine='auto', columns=None, **kwargs):
182182
"""
183183
Load a parquet object from the file path, returning a DataFrame.
184184
@@ -188,6 +188,8 @@ def read_parquet(path, engine='auto', **kwargs):
188188
----------
189189
path : string
190190
File path
191+
columns: list
192+
If not None, only these columns will be read from the file.
191193
engine : {'auto', 'pyarrow', 'fastparquet'}, default 'auto'
192194
Parquet reader library to use. If 'auto', then the option
193195
'io.parquet.engine' is used. If 'auto', then the first
@@ -201,4 +203,4 @@ def read_parquet(path, engine='auto', **kwargs):
201203
"""
202204

203205
impl = get_engine(engine)
204-
return impl.read(path)
206+
return impl.read(path, columns)

pandas/tests/io/test_parquet.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ def test_compression(self, engine, compression):
282282
df = pd.DataFrame({'A': [1, 2, 3]})
283283
self.check_round_trip(df, engine, compression=compression)
284284

285+
def test_read_columns(self, engine, fp):
286+
df = pd.DataFrame({'string': list('abc'),
287+
'int': list(range(1, 4))})
288+
289+
with tm.ensure_clean() as path:
290+
df.to_parquet(path, engine, compression=None)
291+
result = read_parquet(path, engine, columns=["string"])
292+
293+
expected = pd.DataFrame({'string': list('abc')})
294+
tm.assert_frame_equal(result, expected)
295+
285296

286297
class TestParquetPyArrow(Base):
287298

0 commit comments

Comments
 (0)