Skip to content

ENH: Add columns argument to read_feather() (#24025) #24034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ New features
dataframe's indexes from the resulting Parquet file. (:issue:`20768`)
- :meth:`DataFrame.corr` and :meth:`Series.corr` now accept a callable for generic calculation methods of correlation, e.g. histogram intersection (:issue:`22684`)
- :func:`DataFrame.to_string` now accepts ``decimal`` as an argument, allowing the user to specify which decimal separator should be used in the output. (:issue:`23614`)
- :func:`DataFrame.read_feather` now accepts ``columns`` as an argument, allowing the user to specify which columns should be read. (:issue:`24025`)

.. _whatsnew_0240.values_api:

Expand Down
12 changes: 9 additions & 3 deletions pandas/io/feather_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def to_feather(df, path):


@deprecate_kwarg(old_arg_name='nthreads', new_arg_name='use_threads')
def read_feather(path, use_threads=True):
def read_feather(path, columns=None, use_threads=True):
"""
Load a feather-format object from the file path
Expand All @@ -93,6 +93,10 @@ def read_feather(path, use_threads=True):
Parameters
----------
path : string file path, or file-like object
columns : sequence, default None
If not provided, all columns are read
.. versionadded 0.24.0
nthreads : int, default 1
Number of CPU threads to use when reading to pandas.DataFrame
Expand All @@ -116,6 +120,8 @@ def read_feather(path, use_threads=True):
int_use_threads = int(use_threads)
if int_use_threads < 1:
int_use_threads = 1
return feather.read_feather(path, nthreads=int_use_threads)
return feather.read_feather(path, columns=columns,
nthreads=int_use_threads)

return feather.read_feather(path, use_threads=bool(use_threads))
return feather.read_feather(path, columns=columns,
use_threads=bool(use_threads))
17 changes: 15 additions & 2 deletions pandas/tests/io/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def check_error_on_write(self, df, exc):
with ensure_clean() as path:
to_feather(df, path)

def check_round_trip(self, df, **kwargs):
def check_round_trip(self, df, expected=None, **kwargs):

if expected is None:
expected = df

with ensure_clean() as path:
to_feather(df, path)

result = read_feather(path, **kwargs)
assert_frame_equal(result, df)
assert_frame_equal(result, expected)

def test_error(self):

Expand Down Expand Up @@ -74,6 +77,16 @@ def test_stringify_columns(self):
df = pd.DataFrame(np.arange(12).reshape(4, 3)).copy()
self.check_error_on_write(df, ValueError)

def test_read_columns(self):
# GH 24025
df = pd.DataFrame({'col1': list('abc'),
'col2': list(range(1, 4)),
'col3': list('xyz'),
'col4': list(range(4, 7))})
columns = ['col1', 'col3']
self.check_round_trip(df, expected=df[columns],
columns=columns)

def test_unsupported_other(self):

# period
Expand Down