Skip to content
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

BUG: non-nano strftime returns wrong results #50793

Merged
merged 2 commits into from
Jan 18, 2023
Merged
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
13 changes: 12 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,18 @@ class Timestamp(_Timestamp):
>>> ts.strftime('%Y-%m-%d %X')
'2020-03-14 15:32:52'
"""
return datetime.strftime(self, format)
try:
_dt = datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
except ValueError as err:
raise NotImplementedError(
"strftime not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
"For now, please call the components you need (such as `.year` "
"and `.month`) and construct your string from there."
) from err
return _dt.strftime(format)

# Issue 25016.
@classmethod
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,15 @@ def test_utctimetuple():
result = ts.utctimetuple()
expected = time.struct_time((2000, 1, 1, 0, 0, 0, 5, 1, 0))
assert result == expected


def test_negative_dates():
# https://github.com/pandas-dev/pandas/issues/50787
ts = Timestamp("-2000-01-01")
msg = (
"^strftime not yet supported on Timestamps which are outside the range of "
"Python's standard library. For now, please call the components you need "
r"\(such as `.year` and `.month`\) and construct your string from there.$"
)
with pytest.raises(NotImplementedError, match=msg):
ts.strftime("%Y")