Skip to content

Commit c77aad2

Browse files
authored
BUG: constructors (pandas-dev#49875)
1 parent 82d271f commit c77aad2

File tree

7 files changed

+43
-0
lines changed

7 files changed

+43
-0
lines changed

doc/source/whatsnew/v2.0.0.rst

+4
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ Categorical
624624
^^^^^^^^^^^
625625
- Bug in :meth:`Categorical.set_categories` losing dtype information (:issue:`48812`)
626626
- Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would reorder categories when used as a grouper (:issue:`48749`)
627+
- Bug in :class:`Categorical` constructor when constructing from a :class:`Categorical` object and ``dtype="category"`` losing ordered-ness (:issue:`49309`)
628+
-
627629

628630
Datetimelike
629631
^^^^^^^^^^^^
@@ -752,6 +754,8 @@ Reshaping
752754
- Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty :class:`DataFrame` (:issue:`49240`)
753755
- Clarified error message in :func:`merge` when passing invalid ``validate`` option (:issue:`49417`)
754756
- Bug in :meth:`DataFrame.explode` raising ``ValueError`` on multiple columns with ``NaN`` values or empty lists (:issue:`46084`)
757+
- Bug in :meth:`DataFrame.transpose` with ``IntervalDtype`` column with ``timedelta64[ns]`` endpoints (:issue:`44917`)
758+
-
755759

756760
Sparse
757761
^^^^^^

pandas/core/arrays/datetimes.py

+1
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,7 @@ def _sequence_to_dt64ns(
20592059
new_unit = npy_unit_to_abbrev(new_reso)
20602060
new_dtype = np.dtype(f"M8[{new_unit}]")
20612061
data = astype_overflowsafe(data, dtype=new_dtype, copy=False)
2062+
data_unit = get_unit_from_dtype(new_dtype)
20622063
copy = False
20632064

20642065
if data.dtype.byteorder == ">":

pandas/core/dtypes/dtypes.py

+4
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ def _from_values_or_dtype(
278278
# The dtype argument takes precedence over values.dtype (if any)
279279
if isinstance(dtype, str):
280280
if dtype == "category":
281+
if ordered is None and cls.is_dtype(values):
282+
# GH#49309 preserve orderedness
283+
ordered = values.dtype.ordered
284+
281285
dtype = CategoricalDtype(categories, ordered)
282286
else:
283287
raise ValueError(f"Unknown dtype {repr(dtype)}")

pandas/tests/arrays/categorical/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333

3434

3535
class TestCategoricalConstructors:
36+
def test_categorical_from_cat_and_dtype_str_preserve_ordered(self):
37+
# GH#49309 we should preserve orderedness in `res`
38+
cat = Categorical([3, 1], categories=[3, 2, 1], ordered=True)
39+
40+
res = Categorical(cat, dtype="category")
41+
assert res.dtype.ordered
42+
3643
def test_categorical_disallows_scalar(self):
3744
# GH#38433
3845
with pytest.raises(TypeError, match="Categorical input must be list-like"):

pandas/tests/frame/methods/test_transpose.py

+13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@
66
from pandas import (
77
DataFrame,
88
DatetimeIndex,
9+
IntervalIndex,
910
date_range,
11+
timedelta_range,
1012
)
1113
import pandas._testing as tm
1214

1315

1416
class TestTranspose:
17+
def test_transpose_td64_intervals(self):
18+
# GH#44917
19+
tdi = timedelta_range("0 Days", "3 Days")
20+
ii = IntervalIndex.from_breaks(tdi)
21+
ii = ii.insert(-1, np.nan)
22+
df = DataFrame(ii)
23+
24+
result = df.T
25+
expected = DataFrame({i: ii[i : i + 1] for i in range(len(ii))})
26+
tm.assert_frame_equal(result, expected)
27+
1528
def test_transpose_empty_preserves_datetimeindex(self):
1629
# GH#41382
1730
df = DataFrame(index=DatetimeIndex([]))

pandas/tests/indexes/datetimes/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737

3838

3939
class TestDatetimeIndex:
40+
def test_from_dt64_unsupported_unit(self):
41+
# GH#49292
42+
val = np.datetime64(1, "D")
43+
result = DatetimeIndex([val], tz="US/Pacific")
44+
45+
expected = DatetimeIndex([val.astype("M8[s]")], tz="US/Pacific")
46+
tm.assert_index_equal(result, expected)
47+
4048
def test_explicit_tz_none(self):
4149
# GH#48659
4250
dti = date_range("2016-01-01", periods=10, tz="UTC")

pandas/tests/series/test_constructors.py

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454

5555
class TestSeriesConstructors:
56+
def test_from_na_value_and_interval_of_datetime_dtype(self):
57+
# GH#41805
58+
ser = Series([None], dtype="interval[datetime64[ns]]")
59+
assert ser.isna().all()
60+
assert ser.dtype == "interval[datetime64[ns], right]"
61+
5662
def test_infer_with_date_and_datetime(self):
5763
# GH#49341 pre-2.0 we inferred datetime-and-date to datetime64, which
5864
# was inconsistent with Index behavior

0 commit comments

Comments
 (0)