Skip to content

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't pass freq

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new PR is ready (waiting for the question about else)

nanosecond = ts.nanosecond
if nanosecond != 0:
reso = 'nanosecond'
except (ValueError, OutOfBoundsDatetime):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOBDatetime is a subclass of ValueError so this is uneeded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've first try to only use OutOfBoundsDatetime but the test code fails on many VM. Here is an example: https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=37155&view=logs&j=a3a13ea8-7cf0-5bdb-71bb-6ac8830ae35c&t=add65f64-6c25-5783-8fd6-d9aa1b63d9d4&l=471

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

Copy link
Member

Choose a reason for hiding this comment

The 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 ValueError, as that would already cover the OutOfBoundsDatetime case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I got it. I will try a new PR with only ValueError

nanosecond = 0
if dt is NaT:
ordinal = NPY_NAT

Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "])
Copy link
Member

Choose a reason for hiding this comment

The 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):
Expand Down