From e85a4f82b96b2fc49274ffabc9567c41781c5efa Mon Sep 17 00:00:00 2001 From: weikhor Date: Mon, 22 Aug 2022 20:46:59 +0800 Subject: [PATCH 1/6] add test --- pandas/core/frame.py | 3 ++- pandas/tests/frame/constructors/test_from_records.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 93a2c20cd0b74..c811209f6998e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -174,6 +174,7 @@ DatetimeIndex, Index, PeriodIndex, + RangeIndex, default_index, ensure_index, ensure_index_from_sequences, @@ -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 diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index c6d54e28ca1c8..d5322b96c29a0 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -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 @@ -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): From 29c7c12aa2f6c68274152a0099c13a52bcb887f2 Mon Sep 17 00:00:00 2001 From: weikhor Date: Mon, 22 Aug 2022 22:02:19 +0800 Subject: [PATCH 2/6] add test --- pandas/tests/io/test_sql.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index c2c47672b190d..7d237ddf311f1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -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) From 285e9e3cfe5ea5da0c0eeaec918c89dcbb242956 Mon Sep 17 00:00:00 2001 From: weikhor Date: Wed, 24 Aug 2022 20:00:42 +0800 Subject: [PATCH 3/6] add --- pandas/core/frame.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c811209f6998e..e9765cefc0c9a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -174,7 +174,6 @@ DatetimeIndex, Index, PeriodIndex, - RangeIndex, default_index, ensure_index, ensure_index_from_sequences, @@ -2282,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 = RangeIndex(start=0, stop=0, step=1) - arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns, length) return arrays, arr_columns, result_index From 3cd0f6affbee3a5661170ea5cbb4df9bb2166820 Mon Sep 17 00:00:00 2001 From: weikhor Date: Wed, 24 Aug 2022 20:02:57 +0800 Subject: [PATCH 4/6] add gh --- pandas/tests/io/test_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 7d237ddf311f1..fd27c49e653b8 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2974,7 +2974,7 @@ def test_if_exists(self): self.drop_table(table_name) def test_index_consistency_df_and_sql_df(self): - # GH48193 + # GH47608 cur = self.conn.cursor() cur.execute("CREATE TABLE data (id int, val real)") From 55f896022cf075578698ff2dc491121bb25e13af Mon Sep 17 00:00:00 2001 From: weikhor Date: Thu, 25 Aug 2022 20:14:16 +0800 Subject: [PATCH 5/6] revert --- pandas/core/frame.py | 4 ++++ pandas/tests/frame/constructors/test_from_records.py | 9 +++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8695d4838666e..4302b14da6418 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2281,6 +2281,10 @@ 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 diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index d5322b96c29a0..c6d54e28ca1c8 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -396,10 +396,9 @@ 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"], index=index) + exp = DataFrame(data, columns=["a", "b"]) tm.assert_frame_equal(result, exp) # overlap in index/index_names @@ -433,14 +432,12 @@ 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"], index=index) + expected = DataFrame(columns=["a", "b", "c"]) tm.assert_frame_equal(result, expected) result = DataFrame.from_records([], columns=["a", "b", "b"]) - expected = DataFrame(columns=["a", "b", "b"], index=index) + expected = DataFrame(columns=["a", "b", "b"]) tm.assert_frame_equal(result, expected) def test_from_records_empty_with_nonempty_fields_gh3682(self): From 6339cf6df79e39134bf57a225f6d9ac9a1c213f1 Mon Sep 17 00:00:00 2001 From: weikhor Date: Thu, 25 Aug 2022 21:47:39 +0800 Subject: [PATCH 6/6] test --- pandas/tests/io/test_sql.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index fd27c49e653b8..b1a1d5f7ebcb1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2985,8 +2985,7 @@ def test_index_consistency_df_and_sql_df(self): 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 - ) + result = pd.read_sql("SELECT * FROM data WHERE (1 = 0)", self.conn) + expected = DataFrame({"id": [], "val": []}) tm.assert_frame_equal(result, expected)