Skip to content

Commit c53547a

Browse files
mroeschketm9k1
authored andcommitted
BUG: Timestamp retains frequency of input Timestamps (pandas-dev#23503)
1 parent 681dafe commit c53547a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ Datetimelike
11221122
- Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`14187`)
11231123
- Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`)
11241124
- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values (:issue:`23451`)
1125+
- Bug in :class:`Timestamp` constructor which would drop the frequency of an input :class:`Timestamp` (:issue:`22311`)
11251126

11261127
Timedelta
11271128
^^^^^^^^^

pandas/_libs/tslibs/timestamps.pyx

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ from cpython.datetime cimport (datetime,
1616
PyDateTime_IMPORT
1717

1818
from util cimport (is_datetime64_object, is_timedelta64_object,
19-
is_integer_object, is_string_object, is_array)
19+
is_integer_object, is_string_object, is_array,
20+
is_offset_object)
2021

2122
cimport ccalendar
2223
from conversion import tz_localize_to_utc, normalize_i8_timestamps
@@ -734,6 +735,9 @@ class Timestamp(_Timestamp):
734735

735736
if is_string_object(freq):
736737
freq = to_offset(freq)
738+
elif not is_offset_object(freq):
739+
# GH 22311: Try to extract the frequency of a given Timestamp input
740+
freq = getattr(ts_input, 'freq', None)
737741

738742
return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, freq)
739743

pandas/tests/scalar/timestamp/test_timestamp.py

+6
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ def test_construct_with_different_string_format(self, arg):
569569
expected = Timestamp(datetime(2013, 1, 1), tz=pytz.FixedOffset(540))
570570
assert result == expected
571571

572+
def test_construct_timestamp_preserve_original_frequency(self):
573+
# GH 22311
574+
result = Timestamp(Timestamp('2010-08-08', freq='D')).freq
575+
expected = offsets.Day()
576+
assert result == expected
577+
572578

573579
class TestTimestamp(object):
574580

0 commit comments

Comments
 (0)