Skip to content

Commit 77d02bd

Browse files
omatthew98can-anyscale
authored andcommitted
[py312] Fix test_tensor.py (#46669)
To upgrade to py312 we need to upgrade to `pandas>=2.0.0`. This upgrade introduced a breaking change in the syntax of some of our code / tests: > Changed behavior in setting values with df.loc[:, foo] = bar or df.iloc[:, foo] = bar, these now always attempt to set values inplace before falling back to casting ([GH 45333](pandas-dev/pandas#45333)) As a result we needed to update these uses of loc to do direct assignment. <!-- For example: "Closes #1234" --> - [ ] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [ ] I've run `scripts/format.sh` to lint the changes in this PR. - [ ] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [ ] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [ ] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Matthew Owen <[email protected]> Signed-off-by: can <[email protected]>
1 parent 193315b commit 77d02bd

File tree

3 files changed

+464
-130
lines changed

3 files changed

+464
-130
lines changed

python/ray/air/util/data_batch_conversion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def _cast_ndarray_columns_to_tensor_extension(df: "pd.DataFrame") -> "pd.DataFra
319319
with warnings.catch_warnings():
320320
warnings.simplefilter("ignore", category=FutureWarning)
321321
warnings.simplefilter("ignore", category=SettingWithCopyWarning)
322-
df.loc[:, col_name] = TensorArray(col)
322+
df[col_name] = TensorArray(col)
323323
except Exception as e:
324324
raise ValueError(
325325
f"Tried to cast column {col_name} to the TensorArray tensor "
@@ -354,5 +354,5 @@ def _cast_tensor_columns_to_ndarrays(df: "pd.DataFrame") -> "pd.DataFrame":
354354
with warnings.catch_warnings():
355355
warnings.simplefilter("ignore", category=FutureWarning)
356356
warnings.simplefilter("ignore", category=SettingWithCopyWarning)
357-
df.loc[:, col_name] = pd.Series(list(col.to_numpy()))
357+
df[col_name] = list(col.to_numpy())
358358
return df

python/ray/data/tests/test_tensor.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def test_tensors_in_tables_pandas_roundtrip(
564564
ds_df = ds.to_pandas()
565565
expected_df = df + 1
566566
if enable_automatic_tensor_extension_cast:
567-
expected_df.loc[:, "two"] = list(expected_df["two"].to_numpy())
567+
expected_df["two"] = list(expected_df["two"].to_numpy())
568568
pd.testing.assert_frame_equal(ds_df, expected_df)
569569

570570

@@ -585,7 +585,7 @@ def test_tensors_in_tables_pandas_roundtrip_variable_shaped(
585585
ds_df = ds.to_pandas()
586586
expected_df = df + 1
587587
if enable_automatic_tensor_extension_cast:
588-
expected_df.loc[:, "two"] = _create_possibly_ragged_ndarray(
588+
expected_df["two"] = _create_possibly_ragged_ndarray(
589589
expected_df["two"].to_numpy()
590590
)
591591
pd.testing.assert_frame_equal(ds_df, expected_df)
@@ -873,8 +873,8 @@ def test_tensors_in_tables_iter_batches(
873873
)
874874
df = pd.concat([df1, df2], ignore_index=True)
875875
if enable_automatic_tensor_extension_cast:
876-
df.loc[:, "one"] = list(df["one"].to_numpy())
877-
df.loc[:, "two"] = list(df["two"].to_numpy())
876+
df["one"] = list(df["one"].to_numpy())
877+
df["two"] = list(df["two"].to_numpy())
878878
ds = ray.data.from_pandas([df1, df2])
879879
batches = list(ds.iter_batches(batch_size=2, batch_format="pandas"))
880880
assert len(batches) == 3

0 commit comments

Comments
 (0)