Pandas create a month start date range

What's going on here? I need to create a data frame with the start dates of the month, (1-1-2014 to 12-1-2014) fwiw I use the variable fcast_year in another place where I need the end of the month, therefore, the math of the date

from pandas.tseries.offsets import * fcast_yr=pd.to_datetime('2014-12-31') x=(fcast_yr + pd.DateOffset(days= -30)) # to set x to 2014-12-01 d=pd.date_range((x +pd.DateOffset(months=-10)), periods=12, freq='MS') #"MS" means start of month!! print d.values 

Gives the values ​​of the end of the month .... yech !!

 ['2014-01-31T18:00:00.000000000-0600' '2014-02-28T18:00:00.000000000-0600' '2014-03-31T19:00:00.000000000-0500' '2014-04-30T19:00:00.000000000-0500' '2014-05-31T19:00:00.000000000-0500' '2014-06-30T19:00:00.000000000-0500' '2014-07-31T19:00:00.000000000-0500' '2014-08-31T19:00:00.000000000-0500' '2014-09-30T19:00:00.000000000-0500' '2014-10-31T19:00:00.000000000-0500' '2014-11-30T18:00:00.000000000-0600' '2014-12-31T18:00:00.000000000-0600'] 

Using 13.0 pf Pandas

+7
python pandas
source share
1 answer

You do not need to force the timestamp to the beginning of the month; frequency will do it (but your answer is correct).

β€œValues” is just how numpy represents dates (they are UTC).

 In [8]: pd.date_range((Timestamp('20141231') +pd.DateOffset(months=-11)), periods=12, freq='MS') Out[8]: <class 'pandas.tseries.index.DatetimeIndex'> [2014-02-01, ..., 2015-01-01] Length: 12, Freq: MS, Timezone: None 
+8
source share

All Articles