Skip to content

pd.to_datetime ValueError or TypeError #5195

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

Closed
vipatel10 opened this issue Oct 12, 2013 · 5 comments · Fixed by #5197
Closed

pd.to_datetime ValueError or TypeError #5195

vipatel10 opened this issue Oct 12, 2013 · 5 comments · Fixed by #5197
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@vipatel10
Copy link

Hi,

I am using pandas 0.12.0 and numpy 1.7.1.

Not sure if this is the expected functionality or not. I thought if coerce = True or errors='ignore' I wouldn't get this result:

In [372]: td = pd.Series(['May 04', 'Jun 02', 'Dec 11'], index=[1,2,3])

In [373]: td.apply(pd.to_datetime, format='%b %y', errors='ignore', coerce=True)
Out[373]:
1   2004-05-01 00:00:00
2   2002-06-01 00:00:00
3   2011-12-01 00:00:00
dtype: datetime64[ns]

In [374]: td = pd.Series(['May 04', 'Jun 02', ''], index=[1,2,3])

In [375]: td.apply(pd.to_datetime, format='%b %y', errors='ignore', coerce=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-375-0c44a3c36f8e> in <module>()
----> 1 td.apply(pd.to_datetime, format='%b %y', errors='ignore', coerce=True)

C:\Python27\lib\site-packages\pandas\core\series.pyc in apply(self, func, convert_dtype, args, **kwds)
   2534             values = lib.map_infer(values, lib.Timestamp)
   2535
-> 2536         mapped = lib.map_infer(values, f, convert=convert_dtype)
   2537         if isinstance(mapped[0], Series):
   2538             from pandas.core.frame import DataFrame

C:\Python27\lib\site-packages\pandas\lib.pyd in pandas.lib.map_infer (pandas\lib.c:42320)()

C:\Python27\lib\site-packages\pandas\core\series.pyc in <lambda>(x)
   2523
   2524         if kwds or args and not isinstance(func, np.ufunc):
-> 2525             f = lambda x: func(x, *args, **kwds)
   2526         else:
   2527             f = func

C:\Python27\lib\site-packages\pandas\tseries\tools.pyc in to_datetime(arg, errors, dayfirst, utc, box, format, coerce, u
nit)
    127         return _convert_listlike(arg, box=box)
    128
--> 129     return _convert_listlike(np.array([ arg ]), box=box)[0]
    130
    131 class DateParseError(ValueError):

C:\Python27\lib\site-packages\pandas\tseries\tools.pyc in _convert_listlike(arg, box)
    115                 return DatetimeIndex._simple_new(values, None, tz=tz)
    116             except (ValueError, TypeError):
--> 117                 raise e
    118
    119     if arg is None:

ValueError: time data '' does not match format '%b %y'

In [376]:
@vipatel10
Copy link
Author

The problem with pd.to_datetime(td) is it converts "May 04" to May 4, 2013
instead of May 1, 2004.

On Sat, Oct 12, 2013 at 5:49 PM, jreback [email protected] wrote:

that looks like a bug, of course you don't need the apply in any event

In [25]: pd.to_datetime(td)
Out[25]:
1 2013-05-04 00:00:00
2 2013-06-02 00:00:00
3 NaT
dtype: datetime64[ns]


Reply to this email directly or view it on GitHubhttps://github.com//issues/5195#issuecomment-26206785
.

@jreback
Copy link
Contributor

jreback commented Oct 12, 2013

yep...that was a posting bug!

this was not implemented (but now fixed in #5197)

@jreback
Copy link
Contributor

jreback commented Oct 12, 2013

In [1]: td = pd.Series(['May 04', 'Jun 02', ''], index=[1,2,3])

In [2]: pd.to_datetime(td, format='%b %y', coerce=True)
Out[2]: 
1   2004-05-01 00:00:00
2   2002-06-01 00:00:00
3                   NaT
dtype: datetime64[ns]

In [3]: td.apply(lambda x: pd.to_datetime(x, format='%b %y', coerce=True))
Out[3]: 
1   2004-05-01 00:00:00
2   2002-06-01 00:00:00
3                   NaT
dtype: datetime64[ns]

@vipatel10
Copy link
Author

Thanks! I'm new to Github and this whole process. I see you posted a commit, which I believe is a correction of the code. How do I incorporate this into my current pandas installation, if I don't want to wait until the next release? Thanks again.

@jreback
Copy link
Contributor

jreback commented Oct 12, 2013

you can use the latest and greatest version by cloning master

http://pandas.pydata.org/developers.html and https://github.com/pydata/pandas/wiki/Using-Git

haven't merged this yet....soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants