Skip to content

ENH: Expanduser in to_file methods GH9066 #9128

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 1 commit into from
Dec 24, 2014

Conversation

hsperr
Copy link
Contributor

@hsperr hsperr commented Dec 22, 2014

closes #9066

First time contributor, tried to follow all rules as good as possible.
If something is missing please let me know.

As specified in task 9066 tilde in a pathname get resolved into a absolute path.
In case the object is a buffer the buffer will be returned.

I did not add additional tests, since I could not find any module explicitly testing pandas/io/common.py
if that is necessary please let me know how to do that. The current tests are passing on my system and seem to cover the method below.

Best Regards,

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions IO Data IO issues that don't fit into a more specific label labels Dec 22, 2014
@jreback jreback added this to the 0.16.0 milestone Dec 22, 2014
@jreback
Copy link
Contributor

jreback commented Dec 22, 2014

@jorisvandenbossche worth adding a tests_common.py in pandas/io ?

"""
try:
return os.path.expanduser(filepath_or_buffer)
except:
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific exception you are trying to catch here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

while testing the feature it looked like filepath_or_buffer can be a StringIO object which throws an exception in os.path.expanduser(), then I thought I'd stick to the convention like _is_url() or _is_s3_url() where they try to parse it as one and treat it otherwise on except.

Copy link
Member

Choose a reason for hiding this comment

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

Ah... I know we have some bad examples in the existing codebase, but please don't add new blanket except clauses. (Under _is_s3_url, it really should be except ImportError.)

If expanduser does not raise a sane exception, you could use isinstance(filepath_or_buffer, pandas.compat.string_types)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey thank you for the note, I will fix that and use the isinstance version.

@jorisvandenbossche
Copy link
Member

I don't know if this is easy to test (as the home dir depends on where the tests are run, of course can reuse expanduser to just test that the feature is there), but if so, certainly a good idea to add a io/tests/test_common.py

@hsperr
Copy link
Contributor Author

hsperr commented Dec 23, 2014

I now use isinstance instead of try/except to see whether the path might be expandable. I also tried rebasing the new version so it will only be two commits again but maybe my git knowledge is not good enough to do so, if that is necessary please let me know and I will read up/figure it out.

about testing, not sure if its good practice to test against builtins but we can test whether get_filepath_or_buffer() returns the same as os.path.expanduser given a string input and returns the StringIO given a non string input/url.

I will add some tests for that, thanks.

@jorisvandenbossche
Copy link
Member

That is what I meant with "I don't know how easy it is to test", as it is indeed not very useful to test builtin behaviour. But what we can test, it just our API, the fact that the feature is there and will not accidentally be removed later on. So in that case, I think it is not bad to just reuse expanduser, or maybe just assure that something is added to the string)

@@ -40,6 +40,8 @@ Enhancements

.. _whatsnew_0160.enhancements:

- Paths containing a ~ will now be resolved correctly (:issue:`9066`)
Copy link
Member

Choose a reason for hiding this comment

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

I might say something more explicit, e.g., "Paths beginning with ~ will now be expanded to begin with the user's home directory"

@shoyer
Copy link
Member

shoyer commented Dec 24, 2014

@hsperr This looks great! Could you please squash this into a single change? See this page if you're not quite sure what that means.

DOC: Update enhancement in version log for milestone v0.16.0

TST: add tests for pandas.io.common expanduser feature.
@hsperr
Copy link
Contributor Author

hsperr commented Dec 24, 2014

@shoyer Thanks a lot for the link to the rebase page, I indeed did not feel super comfortable with that but I think I managed to squash/fixup everything into a single commit. I also took your version for the changelog, thanks! If there is anything more let me know and hope I can contribute more in the future!

shoyer added a commit that referenced this pull request Dec 24, 2014
ENH: Expanduser in to_file methods GH9066
@shoyer shoyer merged commit def58c9 into pandas-dev:master Dec 24, 2014
@shoyer
Copy link
Member

shoyer commented Dec 24, 2014

Thanks @hsperr !

@hsperr hsperr deleted the expanduser branch December 25, 2014 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expanduser in to_file methods
4 participants