pythonarrayspandasdatedtype

creating an array from a pd.date_range()


I got error while trying to get this array (passed days by beginning each month of the year)

array([  1,  32,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335])

How can I get the desired array from the code below or other code suggestions?

xticks = (pd.date_range('1/1/2015','31/12/2015', freq = 'M') - 1 + pd.Timedelta('1D')).strftime('%-j').astype(int)


TypeError: Addition/subtraction of integers and integer-arrays with DatetimeArray is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`

Solution

  • Create a date range by specifying the frequency as MS (Month-Start)

    pd.date_range('1/1/2015', '31/12/2015', freq='MS').dayofyear.to_numpy()
    

    array([  1,  32,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335],
          dtype=int32)