0

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? I would like to get a datetime type data for further matplotlib plotting.

My code is

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

3 Answers3

3

If you would like to use matplotlib to plot time series figures, here are some options:

(1) extract only the time component using df[col].dt.time, resulting column is string object

import pandas as pd

df = pd.DataFrame({'Time(H:M:S)': ['11:02:03', '11:22:33', '12:00:01']})
df['Time(H:M:S)'] = pd.to_datetime(df['Time(H:M:S)'].astype(str)).dt.time
print(df)

  Time(H:M:S)
0    11:02:03
1    11:22:33
2    12:00:01

(2) add today's date as prefix and convert to datetime object

df['Time(H:M:S)2'] = pd.to_datetime('2022-08-22 ' + df['Time(H:M:S)'].astype(str))
print(df)

  Time(H:M:S)        Time(H:M:S)2
0    11:02:03 2022-08-22 11:02:03
1    11:22:33 2022-08-22 11:22:33
2    12:00:01 2022-08-22 12:00:01

(3) convert to timedelta object, resulting column is timedelta object with prefix "0 days"

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

  Time(H:M:S)        Time(H:M:S)2    Time(H:M:S)3
0    11:02:03 2022-08-22 11:02:03 0 days 11:02:03
1    11:22:33 2022-08-22 11:22:33 0 days 11:22:33
2    12:00:01 2022-08-22 12:00:01 0 days 12:00:01

(4) convert datetime into Epoch timestamp, resulting column is int object

df['Time(H:M:S)4'] = pd.to_datetime(df['Time(H:M:S)'].astype(str)).astype('int64') // int(1e9)
print(df)

  Time(H:M:S) Time(H:M:S)1        Time(H:M:S)2    Time(H:M:S)3  Time(H:M:S)4
0    11:02:03     11:02:03 2022-08-22 11:02:03 0 days 11:02:03    1661166123
1    11:22:33     11:22:33 2022-08-22 11:22:33 0 days 11:22:33    1661167353
2    12:00:01     12:00:01 2022-08-22 12:00:01 0 days 12:00:01    1661169601

The column types are

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      datetime64[ns] 
 2   Time(H:M:S)3  3 non-null      timedelta64[ns]
 3   Time(H:M:S)4  3 non-null      int64
blackraven
  • 5,284
  • 7
  • 19
  • 45
  • if you're looking for time-of-day (and not time durations) this is definitely the right answer. note the result is a vector of python [`datetime.time`](https://docs.python.org/3/library/time.html#module-time) objects – Michael Delgado Aug 20 '22 at 22:10
  • But if I do this, the result would be a 'object', if I convert it to datetime, the year occurs again. – Chauncey Tong Aug 20 '22 at 23:41
  • please let us know your downstream process, ie, what would you like to do next? If you need to do time difference, I've added some examples in my answer, check if that's what you need – blackraven Aug 21 '22 at 02:11
  • I would like to use matplot to plot time series figures and the xtick should be a datetime type – Chauncey Tong Aug 21 '22 at 16:12
  • I guess my second suggestion could work for you? – blackraven Aug 21 '22 at 18:09
  • When I use it to plot figures, the Xticks would be like '22 11:02', while I only want it show '11:02:03', May I know how to do that? – Chauncey Tong Aug 21 '22 at 18:32
  • 1
    I googled for [matplotlib set_xticks time](https://stackoverflow.com/questions/25538520) and got this `ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))` – blackraven Aug 21 '22 at 18:35
  • 1
    It works, I'm starting off on the wrong foot, because what I really need to do is change the format of the X-axis, not the original data set – Chauncey Tong Aug 21 '22 at 19:11
1

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.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
0

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])
Mahmoud Noor
  • 186
  • 12
  • this doesn't work on a datetime series, and doesn't satisfy the OP's desire of converting the `Time(H:M:S)` *string* to a useful numeric value – Michael Delgado Aug 20 '22 at 22:05