Skip to content

Inconsistent index between plain DataFrame and read_sql DataFrame #48193

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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: 0 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2281,10 +2281,6 @@ def maybe_reorder(
length = 0

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([])

arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns, length)
return arrays, arr_columns, result_index

Expand Down
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):
# GH47608
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)