From 80f68b25de420ac48efe94945ff73184b95904a7 Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Fri, 8 May 2020 18:20:24 +0530 Subject: [PATCH 1/6] resolves series.py issue #33327 --- pandas/core/series.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index eb409b432f89c..4f4749ff342d7 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4661,7 +4661,8 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series": if copy: new_values = new_values.copy() - assert isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex)) + if not isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex)): + raise TypeError(f"unsupported Type {self.index}") new_index = self.index.to_timestamp(freq=freq, how=how) return self._constructor(new_values, index=new_index).__finalize__( self, method="to_timestamp" @@ -4688,7 +4689,8 @@ def to_period(self, freq=None, copy=True) -> "Series": if copy: new_values = new_values.copy() - assert isinstance(self.index, ABCDatetimeIndex) + if not isinstance(self.index, ABCDatetimeIndex): + raise TypeError(f"unsupported Type {self.index}") new_index = self.index.to_period(freq=freq) return self._constructor(new_values, index=new_index).__finalize__( self, method="to_period" From e2d14ccfa996c4c667c4f701f356bc20165b5570 Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Tue, 12 May 2020 19:00:02 +0530 Subject: [PATCH 2/6] added tests for timestamp and period TypError checks --- pandas/tests/series/methods/test_to_period.py | 5 +++++ pandas/tests/series/methods/test_to_timestamp.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/pandas/tests/series/methods/test_to_period.py b/pandas/tests/series/methods/test_to_period.py index 28c4aad3edf32..7bda1f5f055d2 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,7 @@ def test_to_period(self): expected = df.copy() expected.columns = exp_idx tm.assert_frame_equal(df.to_period(axis=1), expected) + + # invalid type , #34067 test + with pytest.raises(TypeError): + Series([0]).to_period() diff --git a/pandas/tests/series/methods/test_to_timestamp.py b/pandas/tests/series/methods/test_to_timestamp.py index 44caf1f082a4f..7daa696e74e29 100644 --- a/pandas/tests/series/methods/test_to_timestamp.py +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -2,6 +2,8 @@ from pandas import Series, Timedelta, date_range, period_range, to_datetime import pandas._testing as tm +import pytest +import numpy as np class TestToTimestamp: @@ -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" + + # invalid type , #34067 test + with pytest.raises(TypeError): + Series([0]).to_timestamp() + + with pytest.raises(TypeError): + rng = date_range("1/1/2012", periods=5, freq="M") + ts = Series(np.random.randn(len(rng)), index=rng) + ts.to_timestamp() From 08119d2ebeb6c10a086d7fc878022a2adfd3b07e Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Wed, 20 May 2020 11:43:57 +0530 Subject: [PATCH 3/6] added tests and doc --- doc/source/whatsnew/v1.1.0.rst | 4 ++++ pandas/tests/series/methods/test_to_period.py | 10 ++++++---- pandas/tests/series/methods/test_to_timestamp.py | 14 ++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 4605c14643fa2..8b35b3361abff 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -339,6 +339,10 @@ 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/tests/series/methods/test_to_period.py b/pandas/tests/series/methods/test_to_period.py index 7bda1f5f055d2..7f61575665b30 100644 --- a/pandas/tests/series/methods/test_to_period.py +++ b/pandas/tests/series/methods/test_to_period.py @@ -1,6 +1,4 @@ import numpy as np -import pytest - from pandas import ( DataFrame, DatetimeIndex, @@ -10,6 +8,7 @@ period_range, ) import pandas._testing as tm +import pytest class TestToPeriod: @@ -47,6 +46,9 @@ def test_to_period(self): expected.columns = exp_idx tm.assert_frame_equal(df.to_period(axis=1), expected) + def test_to_period_raises(self, indices): # invalid type , #34067 test - with pytest.raises(TypeError): - Series([0]).to_period() + idx = indices + msg = "unsupported Type RangeIndex" + with pytest.raises(TypeError, match=msg): + Series([idx]).to_period() diff --git a/pandas/tests/series/methods/test_to_timestamp.py b/pandas/tests/series/methods/test_to_timestamp.py index 7daa696e74e29..cd088b792e979 100644 --- a/pandas/tests/series/methods/test_to_timestamp.py +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -1,9 +1,9 @@ from datetime import timedelta from pandas import Series, Timedelta, date_range, period_range, to_datetime +import numpy as np import pandas._testing as tm import pytest -import numpy as np class TestToTimestamp: @@ -55,11 +55,17 @@ def _get_with_delta(delta, freq="A-DEC"): tm.assert_index_equal(result.index, exp_index) assert result.name == "foo" + def test_to_timestamp_raises_type_error_for_rangeindex(self, indices): # invalid type , #34067 test - with pytest.raises(TypeError): - Series([0]).to_timestamp() + idx = indices + msg = "unsupported Type RangeIndex" + with pytest.raises(TypeError, match=msg): + Series([idx]).to_timestamp() - with pytest.raises(TypeError): + def test_to_timestamp_raises_type_error_for_datetimeindex(self): + # invalid type , #34067 test + msg = "unsupported Type .*" + with pytest.raises(TypeError, match=msg): rng = date_range("1/1/2012", periods=5, freq="M") ts = Series(np.random.randn(len(rng)), index=rng) ts.to_timestamp() From 4712f1304cd831d1e4b6c0d86926c3073136c0a3 Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Wed, 20 May 2020 13:32:22 +0530 Subject: [PATCH 4/6] isort files --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/tests/series/methods/test_to_period.py | 3 ++- pandas/tests/series/methods/test_to_timestamp.py | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 8b35b3361abff..0833a9d761be6 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -341,7 +341,7 @@ Backwards incompatible API changes 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`. +- :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/tests/series/methods/test_to_period.py b/pandas/tests/series/methods/test_to_period.py index 7f61575665b30..77dd69d6c8eb9 100644 --- a/pandas/tests/series/methods/test_to_period.py +++ b/pandas/tests/series/methods/test_to_period.py @@ -1,4 +1,6 @@ import numpy as np +import pytest + from pandas import ( DataFrame, DatetimeIndex, @@ -8,7 +10,6 @@ period_range, ) import pandas._testing as tm -import pytest class TestToPeriod: diff --git a/pandas/tests/series/methods/test_to_timestamp.py b/pandas/tests/series/methods/test_to_timestamp.py index cd088b792e979..c9a11c75d2210 100644 --- a/pandas/tests/series/methods/test_to_timestamp.py +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -1,10 +1,11 @@ from datetime import timedelta -from pandas import Series, Timedelta, date_range, period_range, to_datetime import numpy as np -import pandas._testing as tm import pytest +from pandas import Series, Timedelta, date_range, period_range, to_datetime +import pandas._testing as tm + class TestToTimestamp: def test_to_timestamp(self): From 3e3c775641b6d46f27d547d47adf37134785fac2 Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Wed, 20 May 2020 16:42:08 +0530 Subject: [PATCH 5/6] updating with parameterised index --- doc/source/whatsnew/v1.1.0.rst | 6 ++--- pandas/core/series.py | 4 +-- pandas/tests/series/methods/test_to_period.py | 12 +++++---- .../tests/series/methods/test_to_timestamp.py | 25 +++++++------------ 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 0833a9d761be6..6590bef3c52d6 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -339,10 +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`) +- :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 2206b8a5131dd..e107b66d33b1c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4685,7 +4685,7 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series": new_values = new_values.copy() if not isinstance(self.index, PeriodIndex): - raise TypeError(f"unsupported Type {self.index}") + 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" @@ -4713,7 +4713,7 @@ def to_period(self, freq=None, copy=True) -> "Series": new_values = new_values.copy() if not isinstance(self.index, DatetimeIndex): - raise TypeError(f"unsupported Type {self.index}") + 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 77dd69d6c8eb9..5bc4a36498c58 100644 --- a/pandas/tests/series/methods/test_to_period.py +++ b/pandas/tests/series/methods/test_to_period.py @@ -48,8 +48,10 @@ def test_to_period(self): tm.assert_frame_equal(df.to_period(axis=1), expected) def test_to_period_raises(self, indices): - # invalid type , #34067 test - idx = indices - msg = "unsupported Type RangeIndex" - with pytest.raises(TypeError, match=msg): - Series([idx]).to_period() + # 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 c9a11c75d2210..296a1c15619f2 100644 --- a/pandas/tests/series/methods/test_to_timestamp.py +++ b/pandas/tests/series/methods/test_to_timestamp.py @@ -1,9 +1,8 @@ from datetime import timedelta -import numpy as np import pytest -from pandas import Series, Timedelta, date_range, period_range, to_datetime +from pandas import PeriodIndex, Series, Timedelta, date_range, period_range, to_datetime import pandas._testing as tm @@ -56,17 +55,11 @@ def _get_with_delta(delta, freq="A-DEC"): tm.assert_index_equal(result.index, exp_index) assert result.name == "foo" - def test_to_timestamp_raises_type_error_for_rangeindex(self, indices): - # invalid type , #34067 test - idx = indices - msg = "unsupported Type RangeIndex" - with pytest.raises(TypeError, match=msg): - Series([idx]).to_timestamp() - - def test_to_timestamp_raises_type_error_for_datetimeindex(self): - # invalid type , #34067 test - msg = "unsupported Type .*" - with pytest.raises(TypeError, match=msg): - rng = date_range("1/1/2012", periods=5, freq="M") - ts = Series(np.random.randn(len(rng)), index=rng) - ts.to_timestamp() + 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() From a6eaee0997d967c58694310e6b289c8692af1f9e Mon Sep 17 00:00:00 2001 From: Vipul Rai <“vipulrai8891@gmail.com”> Date: Wed, 20 May 2020 17:11:42 +0530 Subject: [PATCH 6/6] remove whitespace from doc --- doc/source/whatsnew/v1.1.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 6590bef3c52d6..a3499f857d158 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -339,8 +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`) +- :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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^