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

astype(object) with dtype datetime64 does wacky things when value is NaT #3414

Closed
lexual opened this issue Apr 22, 2013 · 10 comments
Closed
Assignees
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@lexual
Copy link
Contributor

lexual commented Apr 22, 2013

Similar to #3416, failing in 0.11

Calling astype(object) when the dtype is datetime64 and there are NaT values, sets them to 2262-04-10 00:12:43.145224

I would have thought they should be set to None.

See attached test case.

@lexual
Copy link
Contributor Author

lexual commented Apr 22, 2013

Apparently you can't upload files to github issues?? See below:

# test case for pandas bug with datetime64 conversions.
# NaT is getting converted to
    # 2262-04-10 00:12:43 WTF?!?
# tested against v0.10.1

import pandas as pd
import numpy as np
import datetime


dates = [
    np.datetime64(datetime.date(2013, 1, 1)),
    np.datetime64(datetime.date(2013, 1, 2)),
    np.datetime64(datetime.date(2013, 1, 3)),
]
s = pd.Series(dates, dtype='datetime64')
s.ix[0] = np.nan
print s
print s.dtype
s_o = s.astype(object)
print s_o.dtype
print s_o
print
# 2262-04-10 00:12:43 WTF?!?
print type(s_o.ix[0]), s_o.ix[0]

@jreback
Copy link
Contributor

jreback commented Apr 22, 2013

this was fixed for 0.11

@jreback
Copy link
Contributor

jreback commented Apr 22, 2013

FYI either put code inline or u can use a gist

@jreback
Copy link
Contributor

jreback commented Apr 22, 2013

http://pandas.pydata.org/pandas-docs/dev/whatsnew.html

@jreback
Copy link
Contributor

jreback commented Apr 22, 2013

actually this is a buglet (not the issue you are talking about, that is ok in 0.11, but an odd numpy related one). what version of numpy?

@jreback
Copy link
Contributor

jreback commented Apr 22, 2013

For a workaround - don't wrap in np.datetime64's

In [48]: pd.Series([ datetime.datetime(2013,1,1), datetime.datetime(2013,1,2) ])
Out[48]: 
0   2013-01-01 00:00:00
1   2013-01-02 00:00:00
dtype: datetime64[ns]

@lexual
Copy link
Contributor Author

lexual commented Apr 23, 2013

I see this bug with both numpy 1.6.1 & numpy 1.7.1.

This is a contrived testcase to illustrate my problem, not actually how I came across the bug in my codebase.

Thanks for looking into it @jreback ;)

@ghost ghost assigned jreback Apr 23, 2013
@jreback
Copy link
Contributor

jreback commented Apr 23, 2013

@lexual will get to this with a slew of other np.datetime64 issues (essentially working around numpy bugs, I thought np 1.7.1 should allow this to work, but ok...will fix for 0.12

@jreback
Copy link
Contributor

jreback commented May 8, 2013

@lexual this should be ok in master...

@jreback
Copy link
Contributor

jreback commented May 9, 2013

@lexual

Right now we don't convert types like this (due to various numpy bugs)

pd.Series(dates, dtype='datetime64')
TypeError: cannot convert datetimelike to dtype [datetime64[us]]

And the rest works as expected (the buglet was that the np.datetime64 were not dealt with correct)

In [5]: pd.Series(dates)
Out[5]: 
0   2013-01-01 00:00:00
1   2013-01-02 00:00:00
2   2013-01-03 00:00:00
dtype: datetime64[ns]

In [6]: s = pd.Series(dates)

In [7]: s.ix[0] = np.nan

In [8]: s
Out[8]: 
0                   NaT
1   2013-01-02 00:00:00
2   2013-01-03 00:00:00
dtype: datetime64[ns]

In [9]: s.dtype
Out[9]: dtype('datetime64[ns]')

In [10]: s.astype(object)
Out[10]: 
0                    NaN
1    2013-01-02 00:00:00
2    2013-01-03 00:00:00
dtype: object

closing unless you have further comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

No branches or pull requests

2 participants