diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 44133a1a63597..e2914957d01cd 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -733,11 +733,11 @@ class Timestamp(_Timestamp): if ts.value == NPY_NAT: return NaT - if is_string_object(freq): - freq = to_offset(freq) - elif not is_offset_object(freq): + if freq is None: # GH 22311: Try to extract the frequency of a given Timestamp input freq = getattr(ts_input, 'freq', None) + elif not is_offset_object(freq): + freq = to_offset(freq) return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, freq) diff --git a/pandas/tests/plotting/test_converter.py b/pandas/tests/plotting/test_converter.py index bb976a1e3e81c..eed3679c5bc8c 100644 --- a/pandas/tests/plotting/test_converter.py +++ b/pandas/tests/plotting/test_converter.py @@ -285,11 +285,11 @@ def _assert_less(ts1, ts2): _assert_less(ts, ts + Micro(50)) def test_convert_nested(self): - inner = [Timestamp('2017-01-01', Timestamp('2017-01-02'))] + inner = [Timestamp('2017-01-01'), Timestamp('2017-01-02')] data = [inner, inner] result = self.dtc.convert(data, None, None) expected = [self.dtc.convert(x, None, None) for x in data] - assert result == expected + assert (np.array(result) == expected).all() class TestPeriodConverter(object): diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 9a77a9ccc96c3..7af0b281aeaa5 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -575,6 +575,11 @@ def test_construct_timestamp_preserve_original_frequency(self): expected = offsets.Day() assert result == expected + def test_constructor_invalid_frequency(self): + # GH 22311 + with tm.assert_raises_regex(ValueError, "Invalid frequency:"): + Timestamp('2012-01-01', freq=[]) + class TestTimestamp(object):