Skip to content

Commit

Permalink
BUG: non-nano strftime returns wrong results (#50793)
Browse files Browse the repository at this point in the history
* catch error in strftime

* noop

Co-authored-by: MarcoGorelli <>
  • Loading branch information
MarcoGorelli committed Jan 18, 2023
1 parent 2f44dba commit d3ed92b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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")

0 comments on commit d3ed92b

Please sign in to comment.