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:

JavaScript

Present output

JavaScript

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:

JavaScript

plot 2


Sample Data and imports:

JavaScript

df:

JavaScript

Convert ‘HH:MM’ to_datetime then plot:

JavaScript

plot 1

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

JavaScript

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