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 all 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 @@ -2355,6 +2355,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 +2405,14 @@ class Period(_Period):
value = str(value)
value = value.upper()
dt, reso = parse_time_string(value, freq)
try:
ts = Timestamp(value)
except ValueError:
Copy link
Contributor

Choose a reason for hiding this comment

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

we end up parsing this twice which is not good, yeah i think this needs to be integrated a bit more to parse_time_string.

i guess the currently soln would be ok in the interim, but would need to run asv's on all periods to see what kind of perf hit here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far I read, nanosecond was not accepted into dateutil module. This interim solution could stand for a while.
I stay tuned...

Copy link
Contributor

Choose a reason for hiding this comment

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

As far I read, nanosecond was not accepted into dateutil module. This interim solution could stand for a while.
I stay tuned...

we don't wait for dateutil in this, as it will not likey every support nanosecond because the standard library does not

nanosecond = 0
else:
nanosecond = ts.nanosecond
if nanosecond != 0:
reso = 'nanosecond'
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