Skip to content

Commit c8ad024

Browse files
authored
BUG: yaml.dump(DataFrame) (#44137)
1 parent ddf2f33 commit c8ad024

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ I/O
560560
- Bug in :func:`read_json` not handling non-numpy dtypes correctly (especially ``category``) (:issue:`21892`, :issue:`33205`)
561561
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
562562
- Bug in :func:`read_csv` with :code:`float_precision="round_trip"` which did not skip initial/trailing whitespace (:issue:`43713`)
563+
- Bug in dumping/loading a :class:`DataFrame` with ``yaml.dump(frame)`` (:issue:`42748`)
563564
-
564565

565566
Period

pandas/_libs/internals.pyx

+10-8
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,14 @@ cpdef update_blklocs_and_blknos(
565565
return new_blklocs, new_blknos
566566

567567

568+
def _unpickle_block(values, placement, ndim):
569+
# We have to do some gymnastics b/c "ndim" is keyword-only
570+
571+
from pandas.core.internals.blocks import new_block
572+
573+
return new_block(values, placement, ndim=ndim)
574+
575+
568576
@cython.freelist(64)
569577
cdef class SharedBlock:
570578
"""
@@ -588,14 +596,8 @@ cdef class SharedBlock:
588596
self.ndim = ndim
589597

590598
cpdef __reduce__(self):
591-
# We have to do some gymnastics b/c "ndim" is keyword-only
592-
from functools import partial
593-
594-
from pandas.core.internals.blocks import new_block
595-
596-
args = (self.values, self.mgr_locs.indexer)
597-
func = partial(new_block, ndim=self.ndim)
598-
return func, args
599+
args = (self.values, self.mgr_locs.indexer, self.ndim)
600+
return _unpickle_block, args
599601

600602
cpdef __setstate__(self, state):
601603
from pandas.core.construction import extract_array

pandas/core/internals/array_manager.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ def set_axis(self, axis: int, new_labels: Index) -> None:
170170
def get_dtypes(self):
171171
return np.array([arr.dtype for arr in self.arrays], dtype="object")
172172

173-
# TODO setstate getstate
173+
def __getstate__(self):
174+
return self.arrays, self._axes
175+
176+
def __setstate__(self, state):
177+
self.arrays = state[0]
178+
self._axes = state[1]
174179

175180
def __repr__(self) -> str:
176181
output = type(self).__name__

pandas/tests/test_downstream.py

+13
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ def test_pyarrow(df):
167167
tm.assert_frame_equal(result, df)
168168

169169

170+
def test_yaml_dump(df):
171+
# GH#42748
172+
yaml = import_module("yaml")
173+
174+
dumped = yaml.dump(df)
175+
176+
loaded = yaml.load(dumped, Loader=yaml.Loader)
177+
tm.assert_frame_equal(df, loaded)
178+
179+
loaded2 = yaml.load(dumped, Loader=yaml.UnsafeLoader)
180+
tm.assert_frame_equal(df, loaded2)
181+
182+
170183
def test_missing_required_dependency():
171184
# GH 23868
172185
# To ensure proper isolation, we pass these flags

0 commit comments

Comments
 (0)