Skip to content

BUG : Series.to_timestamp and Series.to_period raise user-facing AssertionError #34067

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

Merged
merged 10 commits into from
May 20, 2020
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/methods/test_to_period.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

from pandas import (
DataFrame,
Expand Down Expand Up @@ -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()
13 changes: 12 additions & 1 deletion pandas/tests/series/methods/test_to_timestamp.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()