|
14 | 14 | @pytest.fixture
|
15 | 15 | def add_interchange_module_for_old_pandas():
|
16 | 16 | if not hasattr(pd.api, "interchange"):
|
17 |
| - pd.api.interchange = mock.MagicMock() |
18 |
| - # to make the following import work: `import pandas.api.interchange` |
19 |
| - with mock.patch.dict( |
20 |
| - "sys.modules", {"pandas.api.interchange": pd.api.interchange} |
21 |
| - ): |
22 |
| - yield |
| 17 | + with mock.patch.object(pd.api, "interchange", mock.MagicMock(), create=True): |
| 18 | + # to make the following import work: `import pandas.api.interchange` |
| 19 | + with mock.patch.dict( |
| 20 | + "sys.modules", {"pandas.api.interchange": pd.api.interchange} |
| 21 | + ): |
| 22 | + yield |
23 | 23 | else:
|
24 | 24 | yield
|
25 | 25 |
|
@@ -250,15 +250,24 @@ def test_build_df_with_index():
|
250 | 250 | assert_frame_equal(tips.reset_index()[out["data_frame"].columns], out["data_frame"])
|
251 | 251 |
|
252 | 252 |
|
| 253 | +@pytest.mark.parametrize("column_names_as_generator", [False, True]) |
253 | 254 | def test_build_df_using_interchange_protocol_mock(
|
254 |
| - add_interchange_module_for_old_pandas, |
| 255 | + add_interchange_module_for_old_pandas, column_names_as_generator |
255 | 256 | ):
|
256 | 257 | class InterchangeDataFrame:
|
257 | 258 | def __init__(self, columns):
|
258 | 259 | self._columns = columns
|
259 | 260 |
|
260 |
| - def column_names(self): |
261 |
| - return self._columns |
| 261 | + if column_names_as_generator: |
| 262 | + |
| 263 | + def column_names(self): |
| 264 | + for col in self._columns: |
| 265 | + yield col |
| 266 | + |
| 267 | + else: |
| 268 | + |
| 269 | + def column_names(self): |
| 270 | + return self._columns |
262 | 271 |
|
263 | 272 | interchange_dataframe = InterchangeDataFrame(
|
264 | 273 | ["petal_width", "sepal_length", "sepal_width"]
|
|
0 commit comments