From 966268c7c12f48a19100f7ba8aac8b32b26ebd99 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Nov 2022 11:06:58 -0800 Subject: [PATCH 1/5] BUG: DatetimeIndex(unsupported_dt64) GH#49292 --- pandas/core/arrays/datetimes.py | 1 + pandas/tests/indexes/datetimes/test_constructors.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 0c90011d28a60..453ed81bc03f0 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2071,6 +2071,7 @@ def _sequence_to_dt64ns( new_unit = npy_unit_to_abbrev(new_reso) new_dtype = np.dtype(f"M8[{new_unit}]") data = astype_overflowsafe(data, dtype=new_dtype, copy=False) + data_unit = get_unit_from_dtype(new_dtype) copy = False if data.dtype.byteorder == ">": diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index d67bc8a132c0c..c5ca4c3958ab3 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -37,6 +37,14 @@ class TestDatetimeIndex: + def test_from_dt64_unsupported_unit(self): + # GH#49292 + val = np.datetime64(1, "D") + result = DatetimeIndex([val], tz="US/Pacific") + + expected = DatetimeIndex([val.astype("M8[s]")], tz="US/Pacific") + tm.assert_index_equal(result, expected) + def test_explicit_tz_none(self): # GH#48659 dti = date_range("2016-01-01", periods=10, tz="UTC") From 72eb14674e5ec0f7828ca5859e31b25021662f1d Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Nov 2022 11:11:57 -0800 Subject: [PATCH 2/5] BUG: DataFrame.T with Interval[td64] GH#44917 --- doc/source/whatsnew/v2.0.0.rst | 3 ++- pandas/tests/frame/methods/test_transpose.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index be81e462c30ea..e35c705587c88 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -747,7 +747,8 @@ Reshaping - Bug in :func:`join` when ``left_on`` or ``right_on`` is or includes a :class:`CategoricalIndex` incorrectly raising ``AttributeError`` (:issue:`48464`) - Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty :class:`DataFrame` (:issue:`49240`) - Clarified error message in :func:`merge` when passing invalid ``validate`` option (:issue:`49417`) -- Bug in :meth:`DataFrame.explode` raising ``ValueError`` on multiple columns with ``NaN`` values or empty lists (:issue:`46084`) +- Bug in :meth:`DataFrame.explode` raising ``ValueError`` on multiple columns with ``NaN`` values or empty lists (:issue:`46084`) Bug in :meth:`DataFrame.transpose` with ``IntervalDtype`` column with ``timedelta64[ns]`` endpoints (:issue:`44917`) +- Sparse ^^^^^^ diff --git a/pandas/tests/frame/methods/test_transpose.py b/pandas/tests/frame/methods/test_transpose.py index 7fca752f2a21e..4f4477ad993ca 100644 --- a/pandas/tests/frame/methods/test_transpose.py +++ b/pandas/tests/frame/methods/test_transpose.py @@ -6,12 +6,25 @@ from pandas import ( DataFrame, DatetimeIndex, + IntervalIndex, date_range, + timedelta_range, ) import pandas._testing as tm class TestTranspose: + def test_transpose_td64_intervals(self): + # GH#44917 + tdi = timedelta_range("0 Days", "3 Days") + ii = IntervalIndex.from_breaks(tdi) + ii = ii.insert(-1, np.nan) + df = DataFrame(ii) + + result = df.T + expected = DataFrame({i: ii[i : i + 1] for i in range(len(ii))}) + tm.assert_frame_equal(result, expected) + def test_transpose_empty_preserves_datetimeindex(self): # GH#41382 df = DataFrame(index=DatetimeIndex([])) From 26ad584438d107396d7c6b406b0f9426d24c3008 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Nov 2022 13:05:52 -0800 Subject: [PATCH 3/5] BUG: Categorical losing orderedness GH#49309 --- doc/source/whatsnew/v2.0.0.rst | 2 ++ pandas/core/dtypes/dtypes.py | 4 ++++ pandas/tests/arrays/categorical/test_constructors.py | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index e35c705587c88..776c609397327 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -622,6 +622,8 @@ Categorical ^^^^^^^^^^^ - Bug in :meth:`Categorical.set_categories` losing dtype information (:issue:`48812`) - Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would reorder categories when used as a grouper (:issue:`48749`) +- Bug in :class:`Categorical` constructor when constructing from a :class:`Categorical` object and ``dtype="category"`` losing ordered-ness (:issue:`49309`) +- Datetimelike ^^^^^^^^^^^^ diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index c4d1d615867a0..7d2e0a4d71c52 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -278,6 +278,10 @@ def _from_values_or_dtype( # The dtype argument takes precedence over values.dtype (if any) if isinstance(dtype, str): if dtype == "category": + if ordered is None and cls.is_dtype(values): + # GH#49309 preserve orderedness + ordered = values.dtype.ordered + dtype = CategoricalDtype(categories, ordered) else: raise ValueError(f"Unknown dtype {repr(dtype)}") diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index 570f04fae2c33..ba33cfe055bb3 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -33,6 +33,13 @@ class TestCategoricalConstructors: + def test_categorical_from_cat_and_dtype_str_preserve_ordered(self): + # GH#49309 we should preserve orderedness in `res` + cat = Categorical([3, 1], categories=[3, 2, 1], ordered=True) + + res = Categorical(cat, dtype="category") + assert res.dtype.ordered + def test_categorical_disallows_scalar(self): # GH#38433 with pytest.raises(TypeError, match="Categorical input must be list-like"): From 3e0b3a74bb0730d9b70e94c449793d92c16f4359 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Nov 2022 14:03:16 -0800 Subject: [PATCH 4/5] TST: for GH#41805 --- pandas/tests/series/test_constructors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index b44f5b699cab4..8b18550dce746 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -53,6 +53,12 @@ class TestSeriesConstructors: + def test_from_na_value_and_interval_of_datetime_dtype(self): + # GH#41805 + ser = Series([None], dtype="interval[datetime64[ns]]") + assert ser.isna().all() + assert ser.dtype == "interval[datetime64[ns], right]" + def test_infer_with_date_and_datetime(self): # GH#49341 pre-2.0 we inferred datetime-and-date to datetime64, which # was inconsistent with Index behavior From d36a44c89740f332e63f9ae67521a4896d42af57 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Nov 2022 15:43:11 -0800 Subject: [PATCH 5/5] fix whatsnew --- doc/source/whatsnew/v2.0.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 776c609397327..cfd5ceacf012d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -749,7 +749,8 @@ Reshaping - Bug in :func:`join` when ``left_on`` or ``right_on`` is or includes a :class:`CategoricalIndex` incorrectly raising ``AttributeError`` (:issue:`48464`) - Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty :class:`DataFrame` (:issue:`49240`) - Clarified error message in :func:`merge` when passing invalid ``validate`` option (:issue:`49417`) -- Bug in :meth:`DataFrame.explode` raising ``ValueError`` on multiple columns with ``NaN`` values or empty lists (:issue:`46084`) Bug in :meth:`DataFrame.transpose` with ``IntervalDtype`` column with ``timedelta64[ns]`` endpoints (:issue:`44917`) +- Bug in :meth:`DataFrame.explode` raising ``ValueError`` on multiple columns with ``NaN`` values or empty lists (:issue:`46084`) +- Bug in :meth:`DataFrame.transpose` with ``IntervalDtype`` column with ``timedelta64[ns]`` endpoints (:issue:`44917`) - Sparse