Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas date_range to generate monthly data at beginning of the month

I'm trying to generate a date range of monthly data where the day is always at the beginning of the month:

pd.date_range(start='1/1/1980', end='11/1/1991', freq='M') 

This generates 1/31/1980, 2/29/1980, and so on. Instead, I just want 1/1/1980, 2/1/1980,...

I've seen other question ask about generating data that is always on a specific day of the month, with answers saying it wasn't possible, but beginning of month surely must be possible!

like image 919
Bunny_Ross Avatar asked Jan 21 '16 05:01

Bunny_Ross


People also ask

What does PD date_range do?

date_range() function. The date_range() function is usede to get a fixed frequency DatetimeIndex. Left bound for generating dates. Right bound for generating dates.

How do you create a date range in pandas?

Specifying the valuesSpecify start and end , with the default daily frequency. Specify start and periods , the number of periods (days). Specify end and periods , the number of periods (days). Specify start , end , and periods ; the frequency is generated automatically (linearly spaced).

How do you create a date range in Python?

Using Pandas to Create a List of Range of Dates in Python. We will use the date_range() function of pandas in which we will pass the start date and the number of days after that (known as periods). Here we have also used the datetime library to format the date so that we can output the date in this format DD-MM-YY .


1 Answers

You can do this by changing the freq argument from 'M' to 'MS':

d = pandas.date_range(start='1/1/1980', end='11/1/1990', freq='MS')     print(d) 

This should now print:

DatetimeIndex(['1980-01-01', '1980-02-01', '1980-03-01', '1980-04-01',                '1980-05-01', '1980-06-01', '1980-07-01', '1980-08-01',                '1980-09-01', '1980-10-01',                 ...                '1990-02-01', '1990-03-01', '1990-04-01', '1990-05-01',                '1990-06-01', '1990-07-01', '1990-08-01', '1990-09-01',                '1990-10-01', '1990-11-01'],               dtype='datetime64[ns]', length=131, freq='MS', tz=None) 

Look into the offset aliases part of the documentation. There it states that 'M' is for the end of the month (month end frequency) while 'MS' for the beginning (month start frequency).

like image 134
Dimitris Fasarakis Hilliard Avatar answered Sep 22 '22 09:09

Dimitris Fasarakis Hilliard