Skip to content

BUG: pathlib.Path in io #16292

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 11 commits into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Performance Improvements
Bug Fixes
~~~~~~~~~

- Bug in using ``pathlib.Path`` object with io functions (:issue:`16291`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localpath too?


Conversion
^^^^^^^^^^

Expand Down
2 changes: 2 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,

handles = list()
f = path_or_buf
# Convert pathlib.Path/py.path.local or string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a blank line

path_or_buf = _stringify_path(path_or_buf)
is_path = isinstance(path_or_buf, compat.string_types)

if compression:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def python_unpickler(path):
compare_element(result, expected, typ)



def test_pickle_v0_14_1():

cat = pd.Categorical(values=['a', 'b', 'c'], ordered=False,
Expand Down Expand Up @@ -299,6 +300,15 @@ def test_pickle_v0_15_2():
tm.assert_categorical_equal(cat, pd.read_pickle(pickle_path))


def test_pickle_path_pathlib():
tm._skip_if_no_pathlib()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of making a specific test can u add a general routine like we have for round_trip_pickle

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then use it here

ideally add tests for all of the io functions (and xfail id they don't work)

and test for localpath as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, let me know if what I just pushed is what you had in mind and I'll add to other io functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep exactly like this!

maybe put a little docs in the testing file above these 3 tests routines.

from pathlib import Path
with tm.ensure_clean('pkl') as path:
df = pd.DataFrame({'a': [1, 2, 3]})
df.to_pickle(Path(path))
result = pd.read_pickle(Path(path))
tm.assert_frame_equal(df, result)

# ---------------------
# test pickle compression
# ---------------------
Expand Down