Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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), specifically Periods, it states that you need to pass in a timespan and a frequency. I'm not quite sure how to use the frequency part regarding to my problem. What frequency would be most appropriate? Is using pd even the best way to do this?

Thanks!

like image 506
a-joyce Avatar asked Jan 23 '17 02:01

a-joyce


People also ask

How do you remove the year from a date in Python?

We can easily get year, month, day, hour, or minute from dates in a column of a pandas dataframe using dt attributes for all columns. For example, we can use df['date']. dt. year to extract only the year from a pandas column that includes the full date.

How do I get month and day in Python?

Approach: In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date class. Create an object of the date class.

How do I get only day in Python?

CurrentDay is what you want. It just prints the day.


2 Answers

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()
s

2011-01-31   2011-01-31
2011-02-28   2011-02-28
2011-03-31   2011-03-31
2011-04-30   2011-04-30
2011-05-31   2011-05-31
2011-06-30   2011-06-30
2011-07-31   2011-07-31
2011-08-31   2011-08-31
2011-09-30   2011-09-30
2011-10-31   2011-10-31
2011-11-30   2011-11-30
2011-12-31   2011-12-31
Freq: 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-31
2011-02-28    02-28
2011-03-31    03-31
2011-04-30    04-30
2011-05-31    05-31
2011-06-30    06-30
2011-07-31    07-31
2011-08-31    08-31
2011-09-30    09-30
2011-10-31    10-31
2011-11-30    11-30
2011-12-31    12-31
Freq: M, dtype: object
like image 94
piRSquared Avatar answered Oct 13 '22 21:10

piRSquared


I believe you are asking if there is an object like datetime whithout a specified year. There doesn't seem to be. Period object has always a year specified: utilizing them won't save memory.

t=pd.Period('03-04',freq='D')
t.year==1
t.month==3
t.day==4

You should use dateTime objects and simply ignore the year they are in. The only obstacle are leap years. For starters you can choose a year 2 years away from any leap year or refer to the Timestamps.isocalender value choosing the starting year with a similar strategy, making your year 12 years away from the closest with 53 weeks (isocalendar has a fixed exact 52 weeks, with a 53 weeks year every 24 years (7 days/week * 4 years/addLeapDay).

TDate15.Date=[d.replace(year=1904) for d in TDate15.Date]#   
TDate15=TDate15.groupby('Date').agg({'Data_Value':['min','max']})
#vs
TDate15=TDate15.groupby([d2015.Date.dt.month,d2015.Date.dt.day]).agg({'Data_Value':['min','max']})
#if you use a reference year as in the first exemple, you get an index of Datetime.
#vs
#using directly groupby day & month, you might need to convert to datetime further, wich would looked at least like:
#TDate15.index=[pd.to_datetime(d,m) for m,d in TDate15.index]
#However I had to do: 
i=list(zip(TDate15.index.labels[0],TDate15.index.labels[1]))
x=[pd.to_datetime( str(TDate05.index.levels[0][m]) + '-' + str(TDate05.index.levels[1][d]) +'-' + '1904',
                format='%m-%d-%Y') \
  for m,d in i]

The complete solution I see is to tweek a bit the datetime class by making it always come back to the same non-leap year. For merging or grouping, strftime does the job for a one time shot. The outputs are strings so for multiple manip you'll need to transform back and forth string-datetime. I hope you don't have a real issue with memory space used because of the 'year'.

like image 34
Etienne Bacave Avatar answered Oct 13 '22 19:10

Etienne Bacave