Skip to content

Test: IntervalIndex.astype("category") doesn't preserve exact interval dtype in categories #48226

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 18 commits into from
Aug 29, 2022
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
DatetimeIndex,
Index,
PeriodIndex,
RangeIndex,
default_index,
ensure_index,
ensure_index_from_sequences,
Expand Down Expand Up @@ -2283,7 +2284,7 @@ def maybe_reorder(
result_index = None
if len(arrays) == 0 and index is None and length == 0:
# for backward compat use an object Index instead of RangeIndex
result_index = Index([])
result_index = RangeIndex(start=0, stop=0, step=1)

arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns, length)
return arrays, arr_columns, result_index
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arrays/categorical/test_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest

from pandas.core.dtypes.dtypes import CategoricalDtype
Expand All @@ -6,6 +7,7 @@
Categorical,
CategoricalIndex,
Index,
IntervalIndex,
Series,
Timestamp,
)
Expand Down Expand Up @@ -134,3 +136,13 @@ def test_iter_python_types_datetime(self):
cat = Categorical([Timestamp("2017-01-01"), Timestamp("2017-01-02")])
assert isinstance(list(cat)[0], Timestamp)
assert isinstance(cat.tolist()[0], Timestamp)

def test_interval_index_types_category(self):
# GH 38316
index = IntervalIndex.from_breaks(np.arange(5, dtype="uint64"))
categoricalIndex = CategoricalIndex(index)

result_dtype = categoricalIndex.dtype.categories.dtype.subtype
expected_type = "uint64"

assert result_dtype == expected_type
9 changes: 6 additions & 3 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,10 @@ def test_from_records_misc_brokenness(self):
# GH#2179

data = {1: ["foo"], 2: ["bar"]}
index = RangeIndex(start=0, stop=0, step=1)

result = DataFrame.from_records(data, columns=["a", "b"])
exp = DataFrame(data, columns=["a", "b"])
exp = DataFrame(data, columns=["a", "b"], index=index)
tm.assert_frame_equal(result, exp)

# overlap in index/index_names
Expand Down Expand Up @@ -432,12 +433,14 @@ def test_from_records_misc_brokenness(self):

def test_from_records_empty(self):
# GH#3562
index = RangeIndex(start=0, stop=0, step=1)

result = DataFrame.from_records([], columns=["a", "b", "c"])
expected = DataFrame(columns=["a", "b", "c"])
expected = DataFrame(columns=["a", "b", "c"], index=index)
tm.assert_frame_equal(result, expected)

result = DataFrame.from_records([], columns=["a", "b", "b"])
expected = DataFrame(columns=["a", "b", "b"])
expected = DataFrame(columns=["a", "b", "b"], index=index)
tm.assert_frame_equal(result, expected)

def test_from_records_empty_with_nonempty_fields_gh3682(self):
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2972,3 +2972,21 @@ def test_if_exists(self):
(5, "E"),
]
self.drop_table(table_name)

def test_index_consistency_df_and_sql_df(self):
# GH48193
cur = self.conn.cursor()

cur.execute("CREATE TABLE data (id int, val real)")
cur.execute("INSERT INTO data VALUES (1, 150.0), (2, 160.0)")
self.conn.commit()

result = pd.read_sql("SELECT * FROM data", self.conn)
expected = DataFrame({"id": [1, 2], "val": [150.0, 160.0]})
tm.assert_frame_equal(result, expected)

result = pd.read_sql("SELECT * FROM data WHERE (1 = 0)", self.conn).astype(
float
)
expected = DataFrame({"id": [], "val": []})
tm.assert_frame_equal(result, expected)