-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API/COMPAT: add pydatetime-style positional args to Timestamp constructor #12482
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
Conversation
cdef _TSObject ts | ||
cdef _Timestamp ts_base | ||
|
||
if offset is not None and type(offset) is int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use is_integer_object(offset)
pls add a whatsnew entry. any luck with accepting the kwargs as well? (or too complicated) |
Should the whatsnew go in for 18.1 or something else? kwargs got too complicated. I wanted to reach a working version of positional parameters first before building kwargs on top of it. I will try out an idea this weekend. |
you can put in 0.18.1; if you finish up before we release we can just move |
8023662
to
31b61e9
Compare
@@ -22,6 +22,14 @@ New features | |||
Enhancements | |||
~~~~~~~~~~~~ | |||
|
|||
- Timestamps accept positional parameters like `datetime.datetime`: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put Timestamp
in double back-ticks
say: can now accept
put a link in for datetime.datetime
back to the constructor (on the python documentation page)
@jorisvandenbossche do we have a nicer way of doing this (as opposed to a straight link), IIRC, sphinx allows us to use an intra-module link (like we do with numpy)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, you can just do :func:
datetime.datetime`` and that will link to the standard library's method
18afb8e
to
b745b7d
Compare
@thejohnfreeman can you rebase / update & move whatsnew to 0.18.2 |
Ok, I rebased. I have time this week to finish this patch. I wrote a longer comment asking a question, but I'll just go ahead and try the signature you gave above.
|
# The parameter list folds together legacy parameter names (the first | ||
# four) and positional parameter names from pydatetime (starting with | ||
# `minute`). | ||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good, this is a nice way to do this.
Current coverage is 84.12%@@ master #12482 diff @@
==========================================
Files 138 138
Lines 50384 50384
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
Hits 42382 42382
Misses 8002 8002
Partials 0 0
|
@thejohnfreeman You will also need to update the Timestamp docstring |
also I believe its trivial to close #11630 (or it might already be done). Add a test and should be straightforward. |
Take a look at my attempt at supporting both positional and keyword arguments. I removed some tests for TypeError; it just means we are more permissive. I'll fix up the doc string and add the other requested tests tomorrow. |
return Timestamp(datetime(year, month, day, hour or 0, | ||
minute or 0, second or 0, microsecond or 0, tzinfo), | ||
tz=tzinfo) | ||
if is_integer_object(offset): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif
whoosh @thejohnfreeman nice constuctor now! yeah prob need some more tests. |
Effectively, we are doing type dependent multiple dispatch here with the |
@shoyer that doesn't make any sense. |
I updated the docstring and added tests. I'm considering this a first draft submitted for approval. If it's approved, I'll squash the commits and we can wait for Travis. |
What about deprecating passing a single object (string or datetime) to It would not be the end of the world to add this alternate constructor but IMO it would not be a positive improvement to have both. If to do do it, let's clearly document the two constructors at the top of the docstring for the class, e.g.,
I think sphinx can process that correctly but we should double check. |
@shoyer why would you blow up a very useful interface?
the entire point is to REDUCE mental effort here. The whole notion of a user having to choose alternate constructors (and I know we had this discussion w.r.t. Sure if things are ambiguous, then you have to choose. In this case it is wholly unambiguous, replicates the existing (python |
I agree -- my proposal is not entirely serious. I do think we need to make hard choices like this to achieve reliable APIs.
Just to prove I'm not crazy, here are some high lights from the first page of a Google search for "Python multiple constructors": I could go on, but you get the point. You'll notice that the universal advice seems to be to skip the magic and use classmethods for alternate constructors instead of overloading |
If we are to add more code paths in the constructor, is it possible to make the entry points more obvious by calling specific |
Hi @kawochen, I too want to make the code paths understandable. I had added some comments to each new path to document its intention. Did they help clarify at all? |
I keep getting lint errors in the automated check. It doesn't say what the error is, and running trace8 locally doesn't give me any indication either.
Any ideas what I need to do to get Travis to pass? |
it's not lint errors but a failed test |
Ah, it expects |
year : int | ||
month : int | ||
day : int | ||
hour : int [optional] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these need to be: int, optional
(of this form)
lgtm. @thejohnfreeman nice explanations in the comments. I think it is clear. comments? |
@thejohnfreeman did you add the test for #11630 (e.g the |
# GH 11630 | ||
self.assertEqual( | ||
repr(Timestamp(2015, 11, 12)), | ||
repr(Timestamp('20151112'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the test for #11630, Github didn't do the best job linking it to your earlier comment.
thanks @thejohnfreeman great PR! |
Thank you! |
git diff upstream/master | flake8 --diff
If more tests are desired, please let me know where to put them. Same for the whatsnew entry. This is my first pull request for Pandas.
cc @jreback