digit50 opened a new issue, #961:
URL: https://github.com/apache/burr/issues/961
The pandas serializer names the parquet file after a hash of the columns,
shape and dtypes only. Two DataFrames with the same schema and shape,
serialized with the same `pandas_kwargs` path, are written to the same file, so
the first serialized state deserializes to the second DataFrame's data.
# Current behavior
`State({"df": DataFrame({"a": [1, 2]})})` and `State({"df": DataFrame({"a":
[9, 9]})})`, serialized with the same `pandas_kwargs={"path": d}`, both point
at the same `df_<hash>.parquet`, and only one file is left on disk.
`State.deserialize` of the first one returns `[9, 9]`.
Persisters and the tracking client serialize state at every step, so a
DataFrame whose values change but whose shape stays the same leaves one file
for all steps, and loading or forking from an earlier step silently gives the
latest DataFrame.
## Stack Traces
None, the wrong data comes back without an error.
## Steps to replicate behavior
1. Save the script below as `repro.py`.
2. `pip install apache-burr==0.43.0 pandas pyarrow`
3. `python repro.py`
```python
import os
import tempfile
import pandas as pd
from burr.core import State
with tempfile.TemporaryDirectory() as d:
kwargs = {"pandas_kwargs": {"path": d}}
s1 = State({"df": pd.DataFrame({"a": [1, 2]})}).serialize(**kwargs)
s2 = State({"df": pd.DataFrame({"a": [9, 9]})}).serialize(**kwargs)
print("path of first :", os.path.basename(s1["df"]["path"]))
print("path of second:", os.path.basename(s2["df"]["path"]))
print("files on disk :", len(os.listdir(d)))
loaded = State.deserialize(s1, **kwargs)["df"]["a"].tolist()
print("observed: first state deserializes to", loaded)
print("expected: first state deserializes to", [1, 2])
```
Output:
```
path of first :
df_b8230ba3a1be1c6ab3b0be1f48cad45cb9522fedfc551436348eeff3aec78854.parquet
path of second:
df_b8230ba3a1be1c6ab3b0be1f48cad45cb9522fedfc551436348eeff3aec78854.parquet
files on disk : 1
observed: first state deserializes to [9, 9]
expected: first state deserializes to [1, 2]
```
## Library & System Information
- apache-burr 0.43.0 (same code on main at f8488d0c)
- pandas 3.0.6, pyarrow 26.0.0
- Python 3.12.3
- Linux
# Expected behavior
Each serialized state keeps its own DataFrame, so deserializing the first
state returns `[1, 2]`.
# Additional context
Cause: `serialize_pandas_df` in `burr/integrations/serde/pandas.py` builds
the hash from `str(value.columns) + str(value.shape) + str(value.dtypes)` (line
53 at f8488d0c) and uses it as the file name (line 57), so the data never
affects the name. The per-step callers are `SQLitePersister.save`
(`burr/core/persistence.py` line 574) and `LocalTrackingClient.post_run_step`
(`burr/tracking/client.py` line 479), and `LocalTrackingClient.load` (lines
648-664) reads the file back for an earlier `sequence_id`.
Proposed fix: include the content in the hash, for example
`pd.util.hash_pandas_object(value, index=True).values`, or the parquet bytes,
or use a unique name per call. Add a test that serializes two same-shape
DataFrames and checks that each one reloads its own data.
I'll open a PR with this fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]