-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Separate out _convert_datetime_to_tsobject #17715
Conversation
Codecov Report
@@ Coverage Diff @@
## master #17715 +/- ##
==========================================
- Coverage 91.27% 91.25% -0.03%
==========================================
Files 163 163
Lines 49783 49783
==========================================
- Hits 45439 45429 -10
- Misses 4344 4354 +10
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #17715 +/- ##
==========================================
- Coverage 91.27% 91.21% -0.06%
==========================================
Files 163 163
Lines 49783 49796 +13
==========================================
- Hits 45439 45421 -18
- Misses 4344 4375 +31
Continue to review full report at Codecov.
|
pandas/_libs/tslib.pyx
Outdated
@@ -728,7 +728,7 @@ class Timestamp(_Timestamp): | |||
# reconstruct & check bounds | |||
ts_input = datetime(dts.year, dts.month, dts.day, dts.hour, dts.min, | |||
dts.sec, dts.us, tzinfo=_tzinfo) | |||
ts = convert_to_tsobject(ts_input, _tzinfo, None, 0, 0) | |||
ts = _convert_datetime_to_tsobject(ts_input, _tzinfo) |
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.
no need to break with spelling convention. call convert_from_datetime_to_tsobject
pandas/_libs/tslib.pyx
Outdated
obj2.dts.day, | ||
obj2.dts.hour, obj2.dts.min, obj2.dts.sec, | ||
obj2.dts.us, obj2.tzinfo) | ||
obj2 = _convert_datetime_to_tsobject(dtime, tz) |
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.
have convert_datetime_to_tsobject
take an optional ps=
to facilitate this type of post-conversion.
also then you don't need a obj2
avoiding more confusion here.
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 idea.
pandas/_libs/tslib.pyx
Outdated
@@ -1577,7 +1589,17 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit, | |||
return obj | |||
else: | |||
# Keep the converter same as PyDateTime's | |||
ts = Timestamp(obj.value, tz=obj.tzinfo) | |||
obj2 = convert_to_tsobject(obj.value, obj.tzinfo, |
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.
this is just a variation on create_datatime_from_ts
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 don't follow.
pandas/_libs/tslib.pyx
Outdated
@@ -1538,12 +1497,64 @@ cdef convert_to_tsobject(object ts, object tz, object unit, | |||
return obj | |||
|
|||
|
|||
cdef _TSObject _convert_datetime_to_tsobject(datetime ts, object tz): | |||
cdef: |
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.
pls pls always add doc-strings
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.
does perf change? (pos or neg)?
pandas/_libs/tslib.pyx
Outdated
int32_t nanos=0): | ||
""" | ||
Extract datetime and int64 from any of: | ||
- python datetime object |
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.
can u put a proper doc string
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.
This is adapted from the convert_to_tsobject
docstring.
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.
take the opportunity to improve it
ts = Timestamp(obj.value, tz=obj.tzinfo) | ||
obj = convert_to_tsobject(obj.value, obj.tzinfo, | ||
None, 0, 0) | ||
dtime = datetime(obj.dts.year, obj.dts.month, obj.dts.day, |
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 the existing routine to do this
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.
Are you referring to create_datetime_from_ts
?
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
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.
create_datetime_from_ts
is a compatibility func specifically for ints_to_pydatetime
(you can tell because it has several unused arguments that are retained to match the signature of create_timestamp_from_ts
).
Replacing a one-line call to datetime(...)
with a call to a one-line func that does exactly the same thing (but with less clarity because of the unused args) doesn't achieve much, with the possible exception of function call overhead.
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 know I wrote it!
yet you are repeating code, I would prefer not to.
with the possible exception of function call overhead.
these take / return objects so the overhead is the same.
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.
Fine.
No significant changes
|
@@ -1577,7 +1614,14 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit, | |||
return obj | |||
else: | |||
# Keep the converter same as PyDateTime's | |||
ts = Timestamp(obj.value, tz=obj.tzinfo) | |||
obj = convert_to_tsobject(obj.value, obj.tzinfo, |
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.
ok so this is getting cumbersome. why don't you make a separate routine to do these 3 (and I guess go back to consrructing datetime). But why does the
ts = Timestamp(obj.value, tz=obj.tzinfo)
not work here?
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.
why don't you make a separate routine to do these 3 (and I guess go back to consrructing datetime)
OK
But why does the
ts = Timestamp(obj.value, tz=obj.tzinfo)
not work here?
It works, but the goal is to disentangle Timestamp
from _TSObject
logic. Or more specifically, to make the dependency go in one direction.
ok ping on green. Still would like to simplify some of this logic generally (as the is pr just moves it around), but can do a bit later (maybe add to TODO). |
Yah. As I look at it, some pieces might be simpler/clearer as methods (or classmethods) of |
Not sure what's up here. Tests started failing when |
ping, though note reversion mentioned above. |
thanks |
* 'master' of github.com:pandas-dev/pandas: (188 commits) Separate out _convert_datetime_to_tsobject (pandas-dev#17715) DOC: remove whatsnew note for xref pandas-dev#17131 BUG: Regression in .loc accepting a boolean Index as an indexer (pandas-dev#17738) DEPR: Deprecate cdate_range and merge into bdate_range (pandas-dev#17691) CLN: replace %s syntax with .format in pandas.core: categorical, common, config, config_init (pandas-dev#17735) Fixed the memory usage explanation of categorical in gotchas from O(nm) to O(n+m) (pandas-dev#17736) TST: add backward compat for offset testing for pickles (pandas-dev#17733) remove unused time conversion funcs (pandas-dev#17711) DEPR: Deprecate convert parameter in take (pandas-dev#17352) BUG:Time Grouper bug fix when applied for list groupers (pandas-dev#17587) BUG: Fix some PeriodIndex resampling issues (pandas-dev#16153) BUG: Fix unexpected sort in groupby (pandas-dev#17621) DOC: Fixed typo in documentation for 'pandas.DataFrame.replace' (pandas-dev#17731) BUG: Fix series rename called with str altering name rather index (GH17407) (pandas-dev#17654) DOC: Add examples for MultiIndex.get_locs + cleanups (pandas-dev#17675) Doc improvements for IntervalIndex and Interval (pandas-dev#17714) BUG: DataFrame sort_values and multiple "by" columns fails to order NaT correctly Last of the timezones funcs (pandas-dev#17669) Add missing file to _pyxfiles, delete commented-out (pandas-dev#17712) update imports of DateParseError, remove unused imports from tslib (pandas-dev#17713) ...
A bunch of calls to
convert_to_tsobject
are made in cases where we already know that the input is adatetime
object. This PR separates out thedatetime
case into a new function_convert_datetime_to_tsobject
.Second, to make the dependency between
_TSObject
andTimestamp
one-way, this removes the three references toTimestamp
that are made inconvert_str_to_tsobject
. Verifying the last of these changes may be non-trivial.This is a prelude to separating
_TSobject
logic out intotslibs.conversion
.git diff upstream/master -u -- "*.py" | flake8 --diff