From b8cc89e57b597a38884b9028dc78fdfef77a11a8 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 16:38:59 -0400 Subject: [PATCH 01/12] bugfix for df instantiation using a dict with a period scalar --- pandas/core/construction.py | 2 +- pandas/core/dtypes/cast.py | 1 - pandas/core/indexes/interval.py | 2 ++ pandas/tests/dtypes/cast/test_infer_dtype.py | 4 +--- pandas/tests/frame/test_constructors.py | 6 ++++++ 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index f145e76046bee..5d6476b89e7c0 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -468,7 +468,7 @@ def sanitize_array( # figure out the dtype from the value (upcast if necessary) if dtype is None: - dtype, value = infer_dtype_from_scalar(value) + dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True) else: # need to possibly convert the value here value = maybe_cast_to_datetime(value, dtype) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index e6b4cb598989b..4a0f15d229409 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -709,7 +709,6 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, elif pandas_dtype: if lib.is_period(val): dtype = PeriodDtype(freq=val.freq) - val = val.ordinal elif lib.is_interval(val): subtype = infer_dtype_from_scalar(val.left, pandas_dtype=True)[0] dtype = IntervalDtype(subtype=subtype) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 5d309ef7cd515..7b7cad790c391 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -609,6 +609,8 @@ def _maybe_convert_i8(self, key): if scalar: # Timestamp/Timedelta key_dtype, key_i8 = infer_dtype_from_scalar(key, pandas_dtype=True) + if lib.is_period(key): + key_i8 = key.ordinal else: # DatetimeIndex/TimedeltaIndex key_dtype, key_i8 = key.dtype, Index(key.asi8) diff --git a/pandas/tests/dtypes/cast/test_infer_dtype.py b/pandas/tests/dtypes/cast/test_infer_dtype.py index 70d38aad951cc..157adacbdfdf7 100644 --- a/pandas/tests/dtypes/cast/test_infer_dtype.py +++ b/pandas/tests/dtypes/cast/test_infer_dtype.py @@ -84,13 +84,11 @@ def test_infer_dtype_from_period(freq, pandas_dtype): if pandas_dtype: exp_dtype = f"period[{freq}]" - exp_val = p.ordinal else: exp_dtype = np.object_ - exp_val = p assert dtype == exp_dtype - assert val == exp_val + assert val == p @pytest.mark.parametrize( diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index c8f5b2b0f6364..330f6bd78375f 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -717,6 +717,12 @@ def test_constructor_period_dict(self): assert df["a"].dtype == a.dtype assert df["b"].dtype == b.dtype + # list of periods + df = DataFrame({"a": pd.Period('2012-01', freq='M'), + "b": pd.Period('2012-02-01', freq='D')}) + assert df["a"].dtype == 'period[M]' + assert df["b"].dtype == 'period[D]' + @pytest.mark.parametrize( "data,dtype", [ From a48fb693e64bd97a66931dae123604536ed3cd2d Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 17:09:45 -0400 Subject: [PATCH 02/12] updated expected behavior of test --- pandas/tests/frame/test_constructors.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 330f6bd78375f..e98d5f0711c85 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -718,10 +718,14 @@ def test_constructor_period_dict(self): assert df["b"].dtype == b.dtype # list of periods - df = DataFrame({"a": pd.Period('2012-01', freq='M'), - "b": pd.Period('2012-02-01', freq='D')}) - assert df["a"].dtype == 'period[M]' - assert df["b"].dtype == 'period[D]' + df = DataFrame( + { + "a": pd.Period("2012-01", freq="M"), + "b": pd.Period("2012-02-01", freq="D"), + } + ) + assert df["a"].dtype == "period[M]" + assert df["b"].dtype == "period[D]" @pytest.mark.parametrize( "data,dtype", From c31a65d62195a8b4e600c55f6eb26bc52a04803f Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 17:11:10 -0400 Subject: [PATCH 03/12] added line in whatsnew --- doc/source/whatsnew/v1.2.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 55570341cf4e8..b70dfcbb135aa 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -273,7 +273,7 @@ Sparse ExtensionArray ^^^^^^^^^^^^^^ -- +- Fixed Bug where :class:`DataFrame` column set to scalar extension type via a dict instantion was considered an object type rather than the extension type (:issue:`35965`) - From 41b1e621047af6f5020001ff52aebf8469eeb302 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 17:52:14 -0400 Subject: [PATCH 04/12] broke out new logic into new test --- pandas/tests/frame/test_constructors.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index e98d5f0711c85..50857eabdaedb 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -717,15 +717,22 @@ def test_constructor_period_dict(self): assert df["a"].dtype == a.dtype assert df["b"].dtype == b.dtype - # list of periods + @pytest.mark.parametrize( + "data,dtype", + [ + (pd.Period("2012-01", freq="M"), 'period[M]'), + (pd.Period("2012-02-01", freq="D"), 'period[D]'), + ], + ) + def test_constructor_period_dict_scalar(self, data, dtype): + # scalar periods df = DataFrame( { - "a": pd.Period("2012-01", freq="M"), - "b": pd.Period("2012-02-01", freq="D"), - } + "a": data, + }, + index=[0] ) - assert df["a"].dtype == "period[M]" - assert df["b"].dtype == "period[D]" + assert df["a"].dtype == dtype @pytest.mark.parametrize( "data,dtype", From ea104f0d33079bdf5d4186eccb6705cc6158395a Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 18:22:47 -0400 Subject: [PATCH 05/12] fixed formatting issue --- pandas/tests/frame/test_constructors.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 50857eabdaedb..2570253af724a 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -720,18 +720,13 @@ def test_constructor_period_dict(self): @pytest.mark.parametrize( "data,dtype", [ - (pd.Period("2012-01", freq="M"), 'period[M]'), - (pd.Period("2012-02-01", freq="D"), 'period[D]'), + (pd.Period("2012-01", freq="M"), "period[M]"), + (pd.Period("2012-02-01", freq="D"), "period[D]"), ], ) def test_constructor_period_dict_scalar(self, data, dtype): # scalar periods - df = DataFrame( - { - "a": data, - }, - index=[0] - ) + df = DataFrame({"a": data,}, index=[0]) assert df["a"].dtype == dtype @pytest.mark.parametrize( From cf01bd00c69cc8bb80c7d4f4c4c1c4739b25212e Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 28 Aug 2020 18:23:58 -0400 Subject: [PATCH 06/12] fixed formatting issue --- pandas/tests/frame/test_constructors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 2570253af724a..9dea2281565bc 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -726,7 +726,7 @@ def test_constructor_period_dict(self): ) def test_constructor_period_dict_scalar(self, data, dtype): # scalar periods - df = DataFrame({"a": data,}, index=[0]) + df = DataFrame({"a": data}, index=[0]) assert df["a"].dtype == dtype @pytest.mark.parametrize( From 30a05342f3ddcd64f2f1cfc608e2d310b36cf3c8 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Fri, 4 Sep 2020 15:44:18 -0400 Subject: [PATCH 07/12] added requested changes --- pandas/tests/frame/test_constructors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 9dea2281565bc..78159d4660945 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -722,6 +722,8 @@ def test_constructor_period_dict(self): [ (pd.Period("2012-01", freq="M"), "period[M]"), (pd.Period("2012-02-01", freq="D"), "period[D]"), + (Interval(left=0, right=5), IntervalDtype("int64")), + (Interval(left=0.1, right=0.5), IntervalDtype("float64")), ], ) def test_constructor_period_dict_scalar(self, data, dtype): @@ -729,6 +731,10 @@ def test_constructor_period_dict_scalar(self, data, dtype): df = DataFrame({"a": data}, index=[0]) assert df["a"].dtype == dtype + expected = DataFrame(index=[0], columns=["a"], data=data) + + tm.assert_frame_equal(df, expected) + @pytest.mark.parametrize( "data,dtype", [ From 0f48b5bcfb3242b9cdecde8d34806e020a70c73a Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Sun, 6 Sep 2020 15:49:52 -0400 Subject: [PATCH 08/12] added test for series construction via a period --- pandas/tests/series/test_constructors.py | 39 +++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index bcf7039ec9039..0fb8c5955a2e7 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -8,16 +8,23 @@ from pandas._libs import iNaT, lib from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype -from pandas.core.dtypes.dtypes import CategoricalDtype +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + DatetimeTZDtype, + IntervalDtype, + PeriodDtype, +) import pandas as pd from pandas import ( Categorical, DataFrame, Index, + Interval, IntervalIndex, MultiIndex, NaT, + Period, Series, Timestamp, date_range, @@ -1075,6 +1082,26 @@ def test_constructor_dict_order(self): expected = Series([1, 0, 2], index=list("bac")) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "data,dtype", + [ + (Period("2020-01"), PeriodDtype("M")), + (Interval(left=0, right=5), IntervalDtype("int64")), + ( + Timestamp("2011-01-01", tz="US/Eastern"), + DatetimeTZDtype(tz="US/Eastern"), + ), + ], + ) + def test_constructor_dict_extension(self, data, dtype): + d = {"a": data} + result = Series(d, index=["a"]) + expected = Series(data, index=["a"], dtype=dtype) + + assert result.dtype == dtype + + tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("value", [2, np.nan, None, float("nan")]) def test_constructor_dict_nan_key(self, value): # GH 18480 @@ -1464,3 +1491,13 @@ def test_constructor_sparse_datetime64(self, values): arr = pd.arrays.SparseArray(values, dtype=dtype) expected = pd.Series(arr) tm.assert_series_equal(result, expected) + + def test_construction_from_ordered_collection(self): + # https://github.com/pandas-dev/pandas/issues/36044 + result = Series({"a": 1, "b": 2}.keys()) + expected = Series(["a", "b"]) + tm.assert_series_equal(result, expected) + + result = Series({"a": 1, "b": 2}.values()) + expected = Series([1, 2]) + tm.assert_series_equal(result, expected) From c5f0454ebb09ce08babc31b01ef935415bbedfb9 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Sun, 6 Sep 2020 18:18:01 -0400 Subject: [PATCH 09/12] alternative implementation --- pandas/core/construction.py | 7 ++++++- pandas/core/dtypes/cast.py | 1 + pandas/core/indexes/interval.py | 2 -- pandas/tests/dtypes/cast/test_infer_dtype.py | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 0993328aef8de..a5cd3874913e4 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -472,7 +472,12 @@ def sanitize_array( # figure out the dtype from the value (upcast if necessary) if dtype is None: - dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True) + dtype, new_value = infer_dtype_from_scalar(value, pandas_dtype=True) + + # infer_dtype_from_scalar converts periods to their ordinal values + # which we do not want here + if not lib.is_period(value): + value = new_value else: # need to possibly convert the value here value = maybe_cast_to_datetime(value, dtype) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index e87e944672eea..7c5aafcbbc7e9 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -709,6 +709,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, elif pandas_dtype: if lib.is_period(val): dtype = PeriodDtype(freq=val.freq) + val = val.ordinal elif lib.is_interval(val): subtype = infer_dtype_from_scalar(val.left, pandas_dtype=True)[0] dtype = IntervalDtype(subtype=subtype) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 7aada1e6eda48..419ff81a2a478 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -612,8 +612,6 @@ def _maybe_convert_i8(self, key): if scalar: # Timestamp/Timedelta key_dtype, key_i8 = infer_dtype_from_scalar(key, pandas_dtype=True) - if lib.is_period(key): - key_i8 = key.ordinal else: # DatetimeIndex/TimedeltaIndex key_dtype, key_i8 = key.dtype, Index(key.asi8) diff --git a/pandas/tests/dtypes/cast/test_infer_dtype.py b/pandas/tests/dtypes/cast/test_infer_dtype.py index 157adacbdfdf7..70d38aad951cc 100644 --- a/pandas/tests/dtypes/cast/test_infer_dtype.py +++ b/pandas/tests/dtypes/cast/test_infer_dtype.py @@ -84,11 +84,13 @@ def test_infer_dtype_from_period(freq, pandas_dtype): if pandas_dtype: exp_dtype = f"period[{freq}]" + exp_val = p.ordinal else: exp_dtype = np.object_ + exp_val = p assert dtype == exp_dtype - assert val == p + assert val == exp_val @pytest.mark.parametrize( From b9607cec0e308bddea5d17f0d6c2ac6eb6ecefc2 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Sun, 6 Sep 2020 18:29:44 -0400 Subject: [PATCH 10/12] Revert "added test for series construction via a period" This reverts commit 0f48b5bcfb3242b9cdecde8d34806e020a70c73a. --- pandas/tests/series/test_constructors.py | 39 +----------------------- 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 0fb8c5955a2e7..bcf7039ec9039 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -8,23 +8,16 @@ from pandas._libs import iNaT, lib from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype -from pandas.core.dtypes.dtypes import ( - CategoricalDtype, - DatetimeTZDtype, - IntervalDtype, - PeriodDtype, -) +from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd from pandas import ( Categorical, DataFrame, Index, - Interval, IntervalIndex, MultiIndex, NaT, - Period, Series, Timestamp, date_range, @@ -1082,26 +1075,6 @@ def test_constructor_dict_order(self): expected = Series([1, 0, 2], index=list("bac")) tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "data,dtype", - [ - (Period("2020-01"), PeriodDtype("M")), - (Interval(left=0, right=5), IntervalDtype("int64")), - ( - Timestamp("2011-01-01", tz="US/Eastern"), - DatetimeTZDtype(tz="US/Eastern"), - ), - ], - ) - def test_constructor_dict_extension(self, data, dtype): - d = {"a": data} - result = Series(d, index=["a"]) - expected = Series(data, index=["a"], dtype=dtype) - - assert result.dtype == dtype - - tm.assert_series_equal(result, expected) - @pytest.mark.parametrize("value", [2, np.nan, None, float("nan")]) def test_constructor_dict_nan_key(self, value): # GH 18480 @@ -1491,13 +1464,3 @@ def test_constructor_sparse_datetime64(self, values): arr = pd.arrays.SparseArray(values, dtype=dtype) expected = pd.Series(arr) tm.assert_series_equal(result, expected) - - def test_construction_from_ordered_collection(self): - # https://github.com/pandas-dev/pandas/issues/36044 - result = Series({"a": 1, "b": 2}.keys()) - expected = Series(["a", "b"]) - tm.assert_series_equal(result, expected) - - result = Series({"a": 1, "b": 2}.values()) - expected = Series([1, 2]) - tm.assert_series_equal(result, expected) From 222eda7c4098cf3891de70c668808bca69ebc1ce Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Sun, 6 Sep 2020 18:31:22 -0400 Subject: [PATCH 11/12] Revert "Revert "added test for series construction via a period"" This reverts commit b9607cec0e308bddea5d17f0d6c2ac6eb6ecefc2. --- pandas/tests/series/test_constructors.py | 39 +++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index bcf7039ec9039..0fb8c5955a2e7 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -8,16 +8,23 @@ from pandas._libs import iNaT, lib from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype -from pandas.core.dtypes.dtypes import CategoricalDtype +from pandas.core.dtypes.dtypes import ( + CategoricalDtype, + DatetimeTZDtype, + IntervalDtype, + PeriodDtype, +) import pandas as pd from pandas import ( Categorical, DataFrame, Index, + Interval, IntervalIndex, MultiIndex, NaT, + Period, Series, Timestamp, date_range, @@ -1075,6 +1082,26 @@ def test_constructor_dict_order(self): expected = Series([1, 0, 2], index=list("bac")) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "data,dtype", + [ + (Period("2020-01"), PeriodDtype("M")), + (Interval(left=0, right=5), IntervalDtype("int64")), + ( + Timestamp("2011-01-01", tz="US/Eastern"), + DatetimeTZDtype(tz="US/Eastern"), + ), + ], + ) + def test_constructor_dict_extension(self, data, dtype): + d = {"a": data} + result = Series(d, index=["a"]) + expected = Series(data, index=["a"], dtype=dtype) + + assert result.dtype == dtype + + tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("value", [2, np.nan, None, float("nan")]) def test_constructor_dict_nan_key(self, value): # GH 18480 @@ -1464,3 +1491,13 @@ def test_constructor_sparse_datetime64(self, values): arr = pd.arrays.SparseArray(values, dtype=dtype) expected = pd.Series(arr) tm.assert_series_equal(result, expected) + + def test_construction_from_ordered_collection(self): + # https://github.com/pandas-dev/pandas/issues/36044 + result = Series({"a": 1, "b": 2}.keys()) + expected = Series(["a", "b"]) + tm.assert_series_equal(result, expected) + + result = Series({"a": 1, "b": 2}.values()) + expected = Series([1, 2]) + tm.assert_series_equal(result, expected) From 320e8d3ce5baac0d212f3980eba7c429e44cc297 Mon Sep 17 00:00:00 2001 From: Justin Essert Date: Sun, 6 Sep 2020 18:31:39 -0400 Subject: [PATCH 12/12] Revert "alternative implementation" This reverts commit c5f0454ebb09ce08babc31b01ef935415bbedfb9. --- pandas/core/construction.py | 7 +------ pandas/core/dtypes/cast.py | 1 - pandas/core/indexes/interval.py | 2 ++ pandas/tests/dtypes/cast/test_infer_dtype.py | 4 +--- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index a5cd3874913e4..0993328aef8de 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -472,12 +472,7 @@ def sanitize_array( # figure out the dtype from the value (upcast if necessary) if dtype is None: - dtype, new_value = infer_dtype_from_scalar(value, pandas_dtype=True) - - # infer_dtype_from_scalar converts periods to their ordinal values - # which we do not want here - if not lib.is_period(value): - value = new_value + dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True) else: # need to possibly convert the value here value = maybe_cast_to_datetime(value, dtype) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 7c5aafcbbc7e9..e87e944672eea 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -709,7 +709,6 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, elif pandas_dtype: if lib.is_period(val): dtype = PeriodDtype(freq=val.freq) - val = val.ordinal elif lib.is_interval(val): subtype = infer_dtype_from_scalar(val.left, pandas_dtype=True)[0] dtype = IntervalDtype(subtype=subtype) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 419ff81a2a478..7aada1e6eda48 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -612,6 +612,8 @@ def _maybe_convert_i8(self, key): if scalar: # Timestamp/Timedelta key_dtype, key_i8 = infer_dtype_from_scalar(key, pandas_dtype=True) + if lib.is_period(key): + key_i8 = key.ordinal else: # DatetimeIndex/TimedeltaIndex key_dtype, key_i8 = key.dtype, Index(key.asi8) diff --git a/pandas/tests/dtypes/cast/test_infer_dtype.py b/pandas/tests/dtypes/cast/test_infer_dtype.py index 70d38aad951cc..157adacbdfdf7 100644 --- a/pandas/tests/dtypes/cast/test_infer_dtype.py +++ b/pandas/tests/dtypes/cast/test_infer_dtype.py @@ -84,13 +84,11 @@ def test_infer_dtype_from_period(freq, pandas_dtype): if pandas_dtype: exp_dtype = f"period[{freq}]" - exp_val = p.ordinal else: exp_dtype = np.object_ - exp_val = p assert dtype == exp_dtype - assert val == exp_val + assert val == p @pytest.mark.parametrize(