Skip to content

Date Operations #10297

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

Closed
ywhcuhk opened this issue Jun 6, 2015 · 3 comments
Closed

Date Operations #10297

ywhcuhk opened this issue Jun 6, 2015 · 3 comments
Labels
Frequency DateOffsets

Comments

@ywhcuhk
Copy link

ywhcuhk commented Jun 6, 2015

IMHO, two date functions from SAS can be very useful and straight forward. I wonder if they can be incorporated in Pandas.

intck()

This function computes the difference between 2 dates/datetime with specified units. For example
Assume: d1=1992-03-20 and d2=1992-06-12. And these are all of date type in SAS.
intck('month', d1, d2) yields 3 (regardless of what day it is). intck('qtr', d1, d2) yields 1. intck('year', d1, d2) yields 0.

intnx()

This function increments a given date by an interval. Assuming d1=1992-03-20, intnx('month', d1, 3, 'end') yields 1992-06-30 (increase month by 3 and return the last day of that month. intnx('month', d1, 3, 'same') yields 1992-06-20 (increase month by 3 and return the same day of that month.


As you can see, these two functions are very flexible and simple. The whole offset system seems to be a little too cumbersome comparing to these 2.


See this question too. Especially the solution offered by @hayd

@hayd
Copy link
Contributor

hayd commented Jun 6, 2015

I think the trickiness is that Months/Years are no longer a set length (in nanonseconds), so it's trickier to divide them... when you subtract you get a timedelta but you can't write this in terms of Month/Year. I think this means you have to check each tuple part to get this info, you can't do anything clever.

Hmmmm, I was going to say that intnx is solved with offsets (this looks like a bug; seems like the way it ought to work):

In [11]: d1
Out[11]: Timestamp('1992-03-20 00:00:00')

In [12]: d1 + pd.offsets.MonthOffset(3)  # increments the days?? BUG??
Out[12]: Timestamp('1992-03-23 00:00:00')

In [13]: d1 + pd.offsets.MonthEnd(3)  # does what you want... modulo 1.
Out[13]: Timestamp('1992-05-31 00:00:00')

The names of these SAS functions are truly awful!!!

@jreback
Copy link
Contributor

jreback commented Jun 6, 2015

You want this. @hayd the first argument to MonthOffset is a day, This is a bug see #7707

In [23]: t = Timestamp('1992-03-20 00:00:00')

In [24]: t
Out[24]: Timestamp('1992-03-20 00:00:00')

In [25]: t+pd.offsets.MonthOffset(months=3)
Out[25]: Timestamp('1992-06-20 00:00:00')

In [26]: t+pd.offsets.MonthEnd(3)
Out[26]: Timestamp('1992-05-31 00:00:00')

@jreback
Copy link
Contributor

jreback commented Jun 6, 2015

I suppose the docs could be improved slightly. pandas provides extreme flexibility. http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects. Though they are pretty complete.

@jreback jreback closed this as completed Jun 6, 2015
@jreback jreback added the Frequency DateOffsets label Jun 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frequency DateOffsets
Projects
None yet
Development

No branches or pull requests

3 participants