Skip to content
Advertisement

Python Dataframe Convert hh:mm:ss object into datetime format

I am trying to convert HH:MM into the datetime format. It converts but it adds an unwanted year 1900. I don’t know why?

My code:

df['HH:MM'] = 
datetime
2019-10-01 08:19:40    08:19:40
2019-10-01 08:20:15    08:20:15
2019-10-01 08:21:29    08:21:29
2019-10-01 08:22:39    08:22:39
2019-10-01 08:29:07    08:29:07
Name: HH:MM, Length: 5, dtype: object

df['HH:MM'] = pd.to_datetime(cdf['HH:MM'], format = '%H:%M:%S', errors='ignore')

Present output

df['HH:MM'] =
datetime
2019-10-01 08:19:40   1900-01-01 08:19:40
2019-10-01 08:20:15   1900-01-01 08:20:15
2019-10-01 08:21:29   1900-01-01 08:21:29
2019-10-01 08:22:39   1900-01-01 08:22:39
2019-10-01 08:29:07   1900-01-01 08:29:07
Name: HH:MM, Length: 5, dtype: datetime64[ns]

Why I need this?

I am plotting HH:MM on the x-axis and value on the y-axis. The x-axis ticks look crazy and we cannot read even after I used plt.gcf().autofmt_xdate(). enter image description here

Advertisement

Answer

autofmt_xdate() will not work if the values are of type str, instead change the type to datetime and manipulate the xaxis through a Locator and a Formatter:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt, dates as mdates

np.random.seed(15)
dr = pd.date_range('2019-10-01 00:00:00', '2019-10-01 23:59', freq='1T')
df = pd.DataFrame({'HH:MM': dr.strftime('%H:%M'),
                   'y': np.random.random(len(dr)) * 10},
                  index=dr)

df['HH:MM'] = pd.to_datetime(df['HH:MM'])
ax = df.plot(kind='scatter', x='HH:MM', y='y', rot=45)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.tight_layout()
plt.show()

plot 2


Sample Data and imports:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt, dates as mdates

np.random.seed(15)
dr = pd.date_range('2019-10-01 00:00:00', '2019-10-01 23:59', freq='1T')
df = pd.DataFrame({'HH:MM': dr.strftime('%H:%M'),
                   'y': np.random.random(len(dr)) * 10},
                  index=dr)

df:

                     HH:MM         y
2019-10-01 00:00:00  00:00  8.488177
2019-10-01 00:01:00  00:01  1.788959
2019-10-01 00:02:00  00:02  0.543632
2019-10-01 00:03:00  00:03  3.615384
2019-10-01 00:04:00  00:04  2.754009

Convert ‘HH:MM’ to_datetime then plot:

df['HH:MM'] = pd.to_datetime(df['HH:MM'])
ax = df.plot(kind='scatter', x='HH:MM', y='y', rot=45)

plot 1

To adjust the number of ticks and format in ‘HH:MM’ format set the Date Locator and the Date Formatter:

ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

Adjust the type of locator or interval to increase or decrease the number of ticks.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement