-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
BUG #34621 added nanosecond support to class Period #34720
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
Changes from 4 commits
91a7c7e
73a6eab
ea9f69c
ab5640f
95d740e
abb0936
8a559cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ from pandas._libs.tslibs.np_datetime cimport ( | |
NPY_FR_D, | ||
NPY_FR_us, | ||
) | ||
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime | ||
|
||
cdef extern from "src/datetime/np_datetime.h": | ||
int64_t npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr, | ||
|
@@ -2355,6 +2356,7 @@ class Period(_Period): | |
|
||
if freq is not None: | ||
freq = cls._maybe_convert_freq(freq) | ||
nanosecond = 0 | ||
|
||
if ordinal is not None and value is not None: | ||
raise ValueError("Only value or ordinal but not both should be " | ||
|
@@ -2404,6 +2406,13 @@ class Period(_Period): | |
value = str(value) | ||
value = value.upper() | ||
dt, reso = parse_time_string(value, freq) | ||
try: | ||
ts = Timestamp(value, freq=freq) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't pass freq There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new PR is ready (waiting for the question about |
||
nanosecond = ts.nanosecond | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if nanosecond != 0: | ||
reso = 'nanosecond' | ||
except (ValueError, OutOfBoundsDatetime): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OOBDatetime is a subclass of ValueError so this is uneeded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've first try to only use pandas/_libs/tslibs/period.pyx:2410: in pandas._libs.tslibs.period.Period.__new__
ts = Timestamp(value, freq=freq)
pandas/_libs/tslibs/timestamps.pyx:848: in pandas._libs.tslibs.timestamps.Timestamp.__new__
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0)
pandas/_libs/tslibs/conversion.pyx:333: in pandas._libs.tslibs.conversion.convert_to_tsobject
return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> raise ValueError("could not convert string to Timestamp")
E ValueError: could not convert string to Timestamp
pandas/_libs/tslibs/conversion.pyx:581: ValueError There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OlivierLuG I think Jeff meant that it should be enough to only use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I got it. I will try a new PR with only |
||
nanosecond = 0 | ||
if dt is NaT: | ||
ordinal = NPY_NAT | ||
|
||
|
@@ -2435,7 +2444,7 @@ class Period(_Period): | |
base = freq_to_dtype_code(freq) | ||
ordinal = period_ordinal(dt.year, dt.month, dt.day, | ||
dt.hour, dt.minute, dt.second, | ||
dt.microsecond, 0, base) | ||
dt.microsecond, 1000*nanosecond, base) | ||
|
||
return cls._from_ordinal(ordinal, freq) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,6 +484,22 @@ def test_period_cons_combined(self): | |
with pytest.raises(ValueError, match=msg): | ||
Period("2011-01", freq="1D1W") | ||
|
||
@pytest.mark.parametrize("day_", ["1970/01/01 ", "2020-12-31 ", "1981/09/13 "]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think the trailing underscores are necessary for these names |
||
@pytest.mark.parametrize("hour_", ["00:00:00", "00:00:01", "23:59:59", "12:00:59"]) | ||
@pytest.mark.parametrize( | ||
"floating_sec_, expected", | ||
[ | ||
(".000000001", 1), | ||
(".000000999", 999), | ||
(".123456789", 789), | ||
(".999999999", 999), | ||
], | ||
) | ||
def test_period_constructor_nanosecond(self, day_, hour_, floating_sec_, expected): | ||
# GH 34621 | ||
result = Period(day_ + hour_ + floating_sec_).start_time.nanosecond | ||
assert result == expected | ||
|
||
|
||
class TestPeriodMethods: | ||
def test_round_trip(self): | ||
|
Uh oh!
There was an error while loading. Please reload this page.