Skip to content

BUG: constructors #49875

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 6 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^
Expand Down Expand Up @@ -748,6 +750,8 @@ Reshaping
- 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`)
-

Sparse
^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ">":
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/methods/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually what we expect? Any consideration to raising?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct. In 2.0 we support resolutions of "s", "ms", "us", and "ns", and when we get something that isn't one of those we cast to the closest supported reso

tm.assert_index_equal(result, expected)

def test_explicit_tz_none(self):
# GH#48659
dti = date_range("2016-01-01", periods=10, tz="UTC")
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@


class TestSeriesConstructors:
def test_from_na_value_and_interval_of_datetime_dtype(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a whatsnew?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think so. this just adds the test, the bug got fixed sometime previously

# 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
Expand Down