Skip to content

Commit 280d04e

Browse files
author
MomIsBestFriend
committed
COMPAT: Added 'Timestamp.fromisocalendar'
1 parent a2bbdb5 commit 280d04e

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

pandas/_libs/tslibs/nattype.pyx

+7
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,14 @@ class NaTType(_NaT):
467467
Return a new Timestamp representing UTC day and time.
468468
"""
469469
)
470+
fromisocalendar = _make_error_func('fromisocalendar', # noqa:E128
471+
"""
472+
Timestamp.fromisocalendar(year, week, day)
470473
474+
Return a new Timestamp corresponding to the
475+
ISO calendar date specified by year, week and day.
476+
"""
477+
)
471478
timestamp = _make_error_func('timestamp', # noqa:E128
472479
"""Return POSIX timestamp as float.""")
473480

pandas/_libs/tslibs/timestamps.pyx

+17
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ class Timestamp(_Timestamp):
336336
"""
337337
return cls(datetime.combine(date, time))
338338

339+
@classmethod
340+
def fromisocalendar(cls, year, week, day):
341+
"""
342+
Timestamp.fromisocalendar(year, week, day)
343+
344+
Return a new Timestamp corresponding to the
345+
ISO calendar date specified by year, week and day.
346+
"""
347+
import pandas.compat as compat
348+
349+
if not compat.PY38:
350+
raise NotImplementedError(
351+
"'fromisocalendar' is not supported for versions earlier than 3.8"
352+
)
353+
354+
return cls(datetime.fromisocalendar(year, week, day))
355+
339356
def __new__(
340357
cls,
341358
object ts_input=_no_input,

pandas/tests/scalar/test_nat.py

-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,6 @@ def test_overlap_public_nat_methods(klass, expected):
297297
# "fromisoformat" was introduced in 3.7
298298
if klass is Timestamp and not compat.PY37:
299299
expected.remove("fromisoformat")
300-
if klass is Timestamp and not compat.PY38:
301-
expected.remove("fromisocalendar")
302300

303301
assert _get_overlap_public_nat_methods(klass) == expected
304302

pandas/tests/scalar/timestamp/test_timestamp.py

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from pandas._libs.tslibs import conversion
1616
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz, get_timezone
17+
import pandas.compat as compat
1718
from pandas.compat.numpy import np_datetime64_compat
1819
from pandas.errors import OutOfBoundsDatetime
1920
import pandas.util._test_decorators as td
@@ -700,6 +701,18 @@ class SubDatetime(datetime):
700701
expected = Timestamp(2000, 1, 1)
701702
assert result == expected
702703

704+
@pytest.mark.skipif(
705+
not compat.PY38,
706+
reason="datetime.fromisocalendar was added in Python version 3.8",
707+
)
708+
def test_constructor_fromisocalendar(self):
709+
# GH 30395
710+
expected_timestamp = Timestamp("2000-01-03 00:00:00")
711+
expected_stdlib = datetime.fromisocalendar(2000, 1, 1)
712+
result = Timestamp.fromisocalendar(2000, 1, 1)
713+
assert result == expected_timestamp
714+
assert result == expected_stdlib
715+
703716

704717
class TestTimestamp:
705718
def test_tz(self):

0 commit comments

Comments
 (0)