Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add days_in_month to Timestamp/DatetimeIndex/.dt #9572

Closed
jreback opened this issue Mar 3, 2015 · 0 comments · Fixed by #9605
Closed

ENH: add days_in_month to Timestamp/DatetimeIndex/.dt #9572

jreback opened this issue Mar 3, 2015 · 0 comments · Fixed by #9605
Labels
Datetime Datetime data dtype Enhancement
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Mar 3, 2015

http://stackoverflow.com/questions/28819470/numbers-of-day-in-month?nah=1#28821925
xref #4640

This should be exposed as a vectorized method in tslib (e.g. take in an ndarray of i8 and return an ndarray)

pd.tslib.monthrange is an unadvertised / undocumented function that handles the days_in_month calculation (adjusting for leap years). This could/should prob be added as a property to Timestamp/DatetimeIndex (and to ``.dt``)

In [34]: df = DataFrame({'date' : pd.date_range('20120101',periods=15,freq='M') })

In [35]: df['year'] = df['date'].dt.year

In [36]: df['month'] = df['date'].dt.month

In [37]: df['days_in_month'] = df.apply(lambda x: pd.tslib.monthrange(x['year'],x['month'])[1], axis=1)

In [38]: df
Out[38]: 
         date  year  month  days_in_month
0  2012-01-31  2012      1             31
1  2012-02-29  2012      2             29
2  2012-03-31  2012      3             31
3  2012-04-30  2012      4             30
4  2012-05-31  2012      5             31
5  2012-06-30  2012      6             30
6  2012-07-31  2012      7             31
7  2012-08-31  2012      8             31
8  2012-09-30  2012      9             30
9  2012-10-31  2012     10             31
10 2012-11-30  2012     11             30
11 2012-12-31  2012     12             31
12 2013-01-31  2013      1             31
13 2013-02-28  2013      2             28
14 2013-03-31  2013      3             31
@jreback jreback added Enhancement Datetime Datetime data dtype labels Mar 3, 2015
@jreback jreback added this to the Next Major Release milestone Mar 3, 2015
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant