How to extract only the month and day from a datetime object?

Candice Zhang picture Candice Zhang · Nov 22, 2016 · Viewed 55.1k times · Source

I was trying to do a scatterplot, and my x-axis needs to be each individual day in a year. I first read in the datafile and get the date column, which are filled with integers like, 19800801. So I convert this integer to datetime by writing:

datetimes_0 = datetime.strptime(str(dates_pitts[0]), '%Y%m%d')

Then I want to extract only the month and day from the datetime object by writing:

s = datetimes_0.strftime("%m%d")

I realized that they return value from strftime is no longer a datetime object so I tried converting it back to datetime object by doing

s0= datetime.strptime(s, '%m%d')

But instead of giving me only the month and day, it gives me back the whole year, month and day. My question is how do I extract a datetime object of only the month and day(both) from a given integer like 19800801?

Answer

Martin Evans picture Martin Evans · Nov 22, 2016

Your approach to converting the date string into a datatime object is correct. It should normally contain as much information as possible.

You are then hoping to only use the day and month for plotting your time based data using matplotlib. The usual approach though is to first convert your dates into datatime objects (which you have done), and to then get matplotlib to convert these values into its own internal representation using the date2num() function.

Give this function the whole datetime object including the year. If your data happened to span more than 12 months (or if it crossed from December to January) you would need it, even if you don't wish it to be displayed.

Next, you can tell matplotlib how to format any ticks on the x-axis using a formatter, in this case a DateFormatter(). So you could for example choose to display month and day for each tick.

In this example, I display the month, with the day on the line below:

from matplotlib import pyplot, dates
from datetime import datetime

data = [("19800801", 16), ("19800810", 10), ("19800901", 15), ("19800905", 14)]

xaxis = [datetime.strptime(d, '%Y%m%d') for d, v in data]
yaxis = [v for d, v in data]

ax = pyplot.gca()
xaxis = dates.date2num(xaxis)    # Convert to maplotlib format
hfmt = dates.DateFormatter('%m\n%d')
ax.xaxis.set_major_formatter(hfmt)

pyplot.xlabel('Date')
pyplot.ylabel('Value')
pyplot.plot(xaxis, yaxis)
pyplot.tight_layout()

pyplot.show()

This would display as follows:

month day xaxis matplotlib demo

You could then extend this to use a DayLocator() which tells matplotlib to place the xticks at exact day locations as follows:

ax.xaxis.set_major_locator(dates.DayLocator())

Giving you:

plot using day locator