Skip to content
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

Add dateutil kwargs to csv2rec #1210

Merged
merged 2 commits into from Apr 2, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/matplotlib/mlab.py
Expand Up @@ -2090,7 +2090,7 @@ def extract(r):

def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',',
converterd=None, names=None, missing='', missingd=None,
use_mrecords=False):
use_mrecords=False, dayfirst=False, yearfirst=False):
Copy link
Member

Choose a reason for hiding this comment

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

Could we just use the builtin datetime for this? (showing my naivety here). If a user specifies a string format as a kwarg, use that with strftime. If not, let dateparser do its (frightening) magic.

Copy link
Member Author

Choose a reason for hiding this comment

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

@pelson That's a nice extension of @efiring's suggestion. I'm happy with that method, too. I'll leave this for a while and let others weigh in with thoughts and opinions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, thinking about it, that wouldn't fix current scripts...

Copy link
Member

Choose a reason for hiding this comment

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

It's hopeless; there is nothing reasonably simple you can do to fix existing usage of this function when applied to ambiguous dates. All we can do is make it easier for users to write new code, or fix old code, so it doesn't blow up in their faces. Dateparser is badly designed for our purposes, but for the time being we are stuck with it. Some method of feeding it the kwargs seems like an essential improvement.

"""
Load data from comma/space/tab delimited file in *fname* into a
numpy record array and return the record array.
Expand Down Expand Up @@ -2129,6 +2129,14 @@ def csv2rec(fname, comments='#', skiprows=0, checkrows=0, delimiter=',',

- *use_mrecords*: if True, return an mrecords.fromrecords record array if any of the data are missing

- *dayfirst*: default is False so that MM-DD-YY has precedence over
DD-MM-YY. See http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47
for further information.

- *yearfirst*: default is False so that MM-DD-YY has precedence over
YY-MM-DD. See http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47
for further information.

If no rows are found, *None* is returned -- see :file:`examples/loadrec.py`
"""

Expand Down Expand Up @@ -2216,7 +2224,7 @@ def mybool(x):

def mydate(x):
# try and return a date object
d = dateparser(x)
d = dateparser(x, dayfirst=dayfirst, yearfirst=yearfirst)

if d.hour>0 or d.minute>0 or d.second>0:
raise ValueError('not a date')
Expand Down