diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 4605c14643fa2..a3499f857d158 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -339,6 +339,8 @@ Backwards incompatible API changes - Combining a ``Categorical`` with integer categories and which contains missing values with a float dtype column in operations such as :func:`concat` or :meth:`~DataFrame.append` will now result in a float column instead of an object dtyped column (:issue:`33607`) +- :meth:`Series.to_timestamp` now raises a ``TypeError`` if the axis is not a :class:`PeriodIndex`. Previously an ``AttributeError`` was raised (:issue:`33327`) +- :meth:`Series.to_period` now raises a ``TypeError`` if the axis is not a :class:`DatetimeIndex`. Previously an ``AttributeError`` was raised (:issue:`33327`) ``MultiIndex.get_indexer`` interprets `method` argument differently ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index 4ba9a0c925a5d..e107b66d33b1c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4684,7 +4684,8 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series": if copy: new_values = new_values.copy() - assert isinstance(self.index, PeriodIndex) + if not isinstance(self.index, PeriodIndex): + raise TypeError(f"unsupported Type {type(self.index).__name__}") new_index = self.index.to_timestamp(freq=freq, how=how) # type: ignore return self._constructor(new_values, index=new_index).__finalize__( self, method="to_timestamp" @@ -4711,7 +4712,8 @@ def to_period(self, freq=None, copy=True) -> "Series": if copy: new_values = new_values.copy() - assert isinstance(self.index, DatetimeIndex) + if not isinstance(self.index, DatetimeIndex): + raise TypeError(f"unsupported Type {type(self.index).__name__}") new_index = self.index.to_period(freq=freq) # type: ignore return self._constructor(new_values, index=new_index).__finalize__( self, method="to_period" diff --git a/pandas/tests/series/methods/test_to_period.py b/pandas/tests/series/methods/test_to_period.py index 28c4aad3edf32..5bc4a36498c58 100644 --- a/pandas/tests/series/methods/test_to_period.py +++ b/pandas/tests/series/methods/test_to_period.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas import ( DataFrame, @@ -45,3 +46,12 @@ def test_to_period(self): expected = df.copy() expected.columns = exp_idx tm.assert_frame_equal(df.to_period(axis=1), expected) + + def test_to_period_raises(self, indices): + # https://github.com/pandas-dev/pandas/issues/33327 + index = indices + ser = Series(index=index, dtype=object) + if not isinstance(index, DatetimeIndex): + msg = f"unsupported Type {type(index).__name__}" + with pytest.raises(TypeError, match=msg): + ser.to_period() diff --git a/pandas/tests/series/methods/test_to_timestamp.py b/pandas/tests/series/methods/test_to_timestamp.py index 44caf1f082a4f..296a1c15619f2 100644 --- a/pandas/tests/series/methods/test_to_timestamp.py +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -1,6 +1,8 @@ from datetime import timedelta -from pandas import Series, Timedelta, date_range, period_range, to_datetime +import pytest + +from pandas import PeriodIndex, Series, Timedelta, date_range, period_range, to_datetime import pandas._testing as tm @@ -52,3 +54,12 @@ def _get_with_delta(delta, freq="A-DEC"): exp_index = exp_index + Timedelta(1, "s") - Timedelta(1, "ns") tm.assert_index_equal(result.index, exp_index) assert result.name == "foo" + + def test_to_timestamp_raises(self, indices): + # https://github.com/pandas-dev/pandas/issues/33327 + index = indices + ser = Series(index=index, dtype=object) + if not isinstance(index, PeriodIndex): + msg = f"unsupported Type {type(index).__name__}" + with pytest.raises(TypeError, match=msg): + ser.to_timestamp()