Home > Software engineering >  Convert to datetime, but how to drop '1900-01-01' in python
Convert to datetime, but how to drop '1900-01-01' in python

Time:08-21

I have a time series only contains hours, minutes, and seconds, when I use datetime convert it into datetime type, it added '1900-01-01' as a date automatically. How to drop the date?

My code is

df['Time(H:M:S)']=pd.to_datetime(df['Time(H:M:S)'], format='%H:%M:%S')

CodePudding user response:

You could extract only the time component using df[col].dt.time

import pandas as pd

df = pd.DataFrame({'Time(H:M:S)': ['01:02:03', '11:22:33', '00:00:01'],
                   'Time(H:M:S)2': ['01:01:00', '11:22:03', '00:01:01']})
df['DateTime'] = pd.to_datetime(df['Time(H:M:S)'])
df['TimeDelta'] = pd.to_timedelta(df['Time(H:M:S)'])
df['Time'] = df['DateTime'].dt.time
df['Difference1'] = (df['TimeDelta'] - pd.to_timedelta(df['Time(H:M:S)2'])).dt.total_seconds()
df['Difference2'] = (df['DateTime'] - pd.to_datetime(df['Time(H:M:S)2'])).dt.total_seconds()

print(df)

  Time(H:M:S) Time(H:M:S)2            DateTime       TimeDelta      Time  Difference1  Difference2
0    01:02:03     01:01:00 2022-08-21 01:02:03 0 days 01:02:03  01:02:03         63.0         63.0
1    11:22:33     11:22:03 2022-08-21 11:22:33 0 days 11:22:33  11:22:33         30.0         30.0
2    00:00:01     00:01:01 2022-08-21 00:00:01 0 days 00:00:01  00:00:01        -60.0        -60.0

print(df.info())

#   Column        Non-Null Count  Dtype          
---  ------        --------------  -----          
 0   Time(H:M:S)   3 non-null      object         
 1   Time(H:M:S)2  3 non-null      object         
 2   DateTime      3 non-null      datetime64[ns] 
 3   TimeDelta     3 non-null      timedelta64[ns]
 4   Time          3 non-null      object         
 5   Difference1   3 non-null      float64        
 6   Difference2   3 non-null      float64 

Or to replace the existing column, this will convert into object type

df['Time(H:M:S)'] = pd.to_datetime(df['Time(H:M:S)']).dt.time

CodePudding user response:

Rather than a datetime, which references a specific moment in time (and must have a date attached), you can use a timedelta, which measures time increments.

In [2]: df = pd.DataFrame({
   ...:     'Time(H:M:S)': [
   ...:         '0:01:00',
   ...:         '0:01:01',
   ...:         '1:00:00',
   ...:         '5:24:04',
   ...:         '26:14:23',
   ...:     ],
   ...: })

In [3]: df['Time(H:M:S)'] = pd.to_timedelta(df['Time(H:M:S)'])

In [4]: df
Out[4]:
      Time(H:M:S)
0 0 days 00:01:00
1 0 days 00:01:01
2 0 days 01:00:00
3 0 days 05:24:04
4 1 days 02:14:23

See the pandas docs on timedeltas for more info.

CodePudding user response:

As @jens said a datetime will always have a date part.

But if you would like to remove the date, you could add .astype(str) to ensure that objects are in string format and apply .apply(lambda), like this:

df['Time(H:M:S)'] = df['Time(H:M:S)'].astype(str).apply(lambda x: x.split(' ')[-1])
  • Related