Skip to content Skip to sidebar Skip to footer

Storing Day And Month Without Year In Python

I simply need to store a month and a date (a birthday) WITHOUT a year value. When looking at the documentation for pandas (mostly because it has useful time manipulation features)

Solution 1:

That isn't how Period's work. They represent a specific period in time. Similar to how Timestamp's represent specific points in time.

You seem to want a generic Month-Day label. You can use strftime to get the string but you'll lose any date manipulation.

Consider the series of timestamps with timestamp indices.

s=pd.date_range('2011-01-31',periods=12,freq='M').to_series()s2011-01-31   2011-01-312011-02-28   2011-02-282011-03-31   2011-03-312011-04-30   2011-04-302011-05-31   2011-05-312011-06-30   2011-06-302011-07-31   2011-07-312011-08-31   2011-08-312011-09-30   2011-09-302011-10-31   2011-10-312011-11-30   2011-11-302011-12-31   2011-12-31Freq:M,dtype:datetime64[ns]

You can get just the month and day like this (see this site for a summary of strftime)

s.dt.strftime('%m-%d')2011-01-31    01-312011-02-28    02-282011-03-31    03-312011-04-30    04-302011-05-31    05-312011-06-30    06-302011-07-31    07-312011-08-31    08-312011-09-30    09-302011-10-31    10-312011-11-30    11-302011-12-31    12-31Freq:M,dtype:object

Post a Comment for "Storing Day And Month Without Year In Python"