Tikiyetti
Tikiyetti

Reputation: 485

Python to convert these dates to the right format for plotting

I have a long list of dates formatted like the below:

8-JAN-18
6-JAN-18
24-DEC-17
19-DEC-17
12-JAN-16
9-JAN-16
13-NOV-15
28-OCT-15

I am trying to plot a yearly histogram of these dates; would I be able to use pandas to plot these as is- or do I need to convert them first? And if so, how do I convert these to the right format?

I can use a python script to read in the file and iterate over every line, replacing JAN, FEB, MAR, etc.. with it's respective number. Alternatively I can use sed/awk to do the same, remove the dashes, and add as '20' to the years, but is there a quicker way of doing it with pandas?

Upvotes: 0

Views: 37

Answers (1)

DYZ
DYZ

Reputation: 57033

Pandas has excellent support for dates (and times). Read the data from your file (say, "dates.txt") and tell Pandas that the first column is a date. It will figure out the format:

df = pd.read_csv("dates.txt", header=None)
df[0] = pd.to_datetime(df[0])

Upvotes: 1

Related Questions