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:
JavaScript
x
11
11
1
df['HH:MM'] =
2
datetime
3
2019-10-01 08:19:40 08:19:40
4
2019-10-01 08:20:15 08:20:15
5
2019-10-01 08:21:29 08:21:29
6
2019-10-01 08:22:39 08:22:39
7
2019-10-01 08:29:07 08:29:07
8
Name: HH:MM, Length: 5, dtype: object
9
10
df['HH:MM'] = pd.to_datetime(cdf['HH:MM'], format = '%H:%M:%S', errors='ignore')
11
Present output
JavaScript
1
9
1
df['HH:MM'] =
2
datetime
3
2019-10-01 08:19:40 1900-01-01 08:19:40
4
2019-10-01 08:20:15 1900-01-01 08:20:15
5
2019-10-01 08:21:29 1900-01-01 08:21:29
6
2019-10-01 08:22:39 1900-01-01 08:22:39
7
2019-10-01 08:29:07 1900-01-01 08:29:07
8
Name: HH:MM, Length: 5, dtype: datetime64[ns]
9
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()
.
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
:
JavaScript
1
17
17
1
import numpy as np
2
import pandas as pd
3
from matplotlib import pyplot as plt, dates as mdates
4
5
np.random.seed(15)
6
dr = pd.date_range('2019-10-01 00:00:00', '2019-10-01 23:59', freq='1T')
7
df = pd.DataFrame({'HH:MM': dr.strftime('%H:%M'),
8
'y': np.random.random(len(dr)) * 10},
9
index=dr)
10
11
df['HH:MM'] = pd.to_datetime(df['HH:MM'])
12
ax = df.plot(kind='scatter', x='HH:MM', y='y', rot=45)
13
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
14
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
15
plt.tight_layout()
16
plt.show()
17
Sample Data and imports:
JavaScript
1
10
10
1
import numpy as np
2
import pandas as pd
3
from matplotlib import pyplot as plt, dates as mdates
4
5
np.random.seed(15)
6
dr = pd.date_range('2019-10-01 00:00:00', '2019-10-01 23:59', freq='1T')
7
df = pd.DataFrame({'HH:MM': dr.strftime('%H:%M'),
8
'y': np.random.random(len(dr)) * 10},
9
index=dr)
10
df
:
JavaScript
1
7
1
HH:MM y
2
2019-10-01 00:00:00 00:00 8.488177
3
2019-10-01 00:01:00 00:01 1.788959
4
2019-10-01 00:02:00 00:02 0.543632
5
2019-10-01 00:03:00 00:03 3.615384
6
2019-10-01 00:04:00 00:04 2.754009
7
Convert ‘HH:MM’ to_datetime
then plot:
JavaScript
1
3
1
df['HH:MM'] = pd.to_datetime(df['HH:MM'])
2
ax = df.plot(kind='scatter', x='HH:MM', y='y', rot=45)
3
To adjust the number of ticks and format in ‘HH:MM’ format set the Date Locator and the Date Formatter:
JavaScript
1
3
1
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
2
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
3
Adjust the type of locator or interval to increase or decrease the number of ticks.