Skip to content

NaTType does not support .time() #11453

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
nathholf opened this issue Oct 28, 2015 · 2 comments
Closed

NaTType does not support .time() #11453

nathholf opened this issue Oct 28, 2015 · 2 comments
Labels
API Design Datetime Datetime data dtype

Comments

@nathholf
Copy link

I am currently writing a script to take data from an API and save it to an Excel file for non-technical users who need to access the data. One of the requirements is to have separate date and time column, rather than a single datetime column.

I am using pandas 0.17.0.

import pandas as pd

# creating datetime series
dates = pd.date_range('2015-01-01 00:00', '2015-01-01 03:00', freq='H')
dates = pd.Series(dates)
# setting one record to NaT
dates.iloc[0] = pd.NaT
# converting to date 
dates.apply(lambda x: x.date()) # ignores the NaT
# converting to time
dates.apply(lambda x: x.time()) # fails with error "NaTType does not support time"

I expected NaTType to behave in the same way for converting into a time as it did for a date i.e. returning NaT.

The full error is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-98-b34fccfc4c06> in <module>()
      9 dates.apply(lambda x: x.date()) # ignores the NaT
     10 # converting to time
---> 11 dates.apply(lambda x: x.time()) # fails with error "NaTType does not support time"

C:\Users\Nathan\Anaconda\lib\site-packages\pandas\core\series.pyc in apply(self, func, convert_dtype, args, **kwds)
   2158             values = lib.map_infer(values, lib.Timestamp)
   2159 
-> 2160         mapped = lib.map_infer(values, f, convert=convert_dtype)
   2161         if len(mapped) and isinstance(mapped[0], Series):
   2162             from pandas.core.frame import DataFrame

pandas\src\inference.pyx in pandas.lib.map_infer (pandas\lib.c:62187)()
@jreback
Copy link
Contributor

jreback commented Oct 28, 2015

We had this debate here: #10372

time is a special animal and should be an obvious error and not glossed over if it returned np.nan (as does .date) on a NaTType.

Further using .apply is very inefficient. You can catch the exception and return np.nan if you'd like, or simply use the methods designed for this.

In [55]: dates
Out[55]: 
0                   NaT
1   2015-01-01 01:00:00
2   2015-01-01 02:00:00
3   2015-01-01 03:00:00
dtype: datetime64[ns]

In [56]: dates.dt.date
Out[56]: 
0           NaN
1    2015-01-01
2    2015-01-01
3    2015-01-01
dtype: object

In [57]: dates.dt.time
Out[57]: 
0         NaN
1    01:00:00
2    02:00:00
3    03:00:00
dtype: object

@jreback jreback closed this as completed Oct 28, 2015
@jreback jreback added Datetime Datetime data dtype API Design labels Oct 28, 2015
@nathholf
Copy link
Author

Thanks @jreback this makes sense.

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

No branches or pull requests

2 participants