diff --git a/ibis/formats/pandas.py b/ibis/formats/pandas.py index b2efdd21a7349..ea67c9ad75378 100644 --- a/ibis/formats/pandas.py +++ b/ibis/formats/pandas.py @@ -109,14 +109,11 @@ def infer_table(cls, df): @classmethod def convert_table(cls, df, schema): - if len(schema) != len(df.columns): - raise ValueError( - "schema column count does not match input data column count" - ) + if schema.names != tuple(df.columns): + raise ValueError("schema names don't match input data columns") columns = { - name: cls.convert_column(series, dtype) - for (name, dtype), (_, series) in zip(schema.items(), df.items()) + name: cls.convert_column(df[name], dtype) for name, dtype in schema.items() } df = pd.DataFrame(columns) diff --git a/ibis/formats/tests/test_pandas.py b/ibis/formats/tests/test_pandas.py index 671d3be1b99c5..13c33da6bcfea 100644 --- a/ibis/formats/tests/test_pandas.py +++ b/ibis/formats/tests/test_pandas.py @@ -433,3 +433,10 @@ def test_convert_dataframe_with_timezone(): desired_schema = ibis.schema(dict(time='timestamp("EST")')) result = PandasData.convert_table(df.copy(), desired_schema) tm.assert_frame_equal(expected, result) + + +def test_schema_doesnt_match_input_columns(): + df = pd.DataFrame({"x": [1], "y": [2]}) + schema = sch.Schema({"a": "int64", "b": "int64"}) + with pytest.raises(ValueError, match="schema names don't match"): + PandasData.convert_table(df, schema)