简体   繁体   中英

Converting pandas object to timedelta results in NaT

I have a DataFrame with three columns, one date and two times. It's like this:

         date hour_in hour_out
0  01/06/2016        08:15      19:37   
1  02/06/2016        08:26      17:31   
2  03/06/2016        08:08      21:31

I'm trying to convert hour_in and hour_out to timedelta using this code (which is based on an answer on this question Dates from 1900-01-01 are added to my 'Time' after using df['Time'] = pd.to_datetime(phData['Time'], format='%H:%M:%S') ):

df['hora_entrada'] = pd.to_timedelta(df['hora_entrada'], errors='coerce')
df['hora_saida']  = pd.to_timedelta(df['hora_saida'] , errors='coerce')

After the cast, my column is converted to the correct dtype timedelta64[ns] , but all the values are set to NaT . My df.info() returns this:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 439 entries, 0 to 438
Data columns (total 4 columns):
data            439 non-null datetime64[ns]
hour_in         0 non-null timedelta64[ns]
hour_out        0 non-null timedelta64[ns]
dtypes: datetime64[ns](1), timedelta64[ns](2)

And the data output is like this:

          data hora_entrada hora_saida
0   2016-06-01          NaT        NaT
1   2016-06-02          NaT        NaT
2   2016-06-03          NaT        NaT

I've tried to convert the time columns to datetime and then to timedelta but I got strange results. Here's an example:

          data          hora_entrada            hora_saida
0   2016-06-01 -25567 days +08:15:00 -25567 days +19:37:00
1   2016-06-02 -25567 days +08:26:00 -25567 days +17:31:00
2   2016-06-03 -25567 days +08:08:00 -25567 days +21:31:00

I think it's because when I convert it to datetime it's appended to the hour a date 1900-01-01 .

Consider the following approach:

In [24]: pd.to_timedelta(df.hour_in + ':00', errors='coerce')
Out[24]:
0   08:15:00
1   08:26:00
2   08:08:00
Name: hour_in, dtype: timedelta64[ns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM