Skip to content

Immerrr fix categoricalblock pickling #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,4 @@ Bug Fixes
- Regression in ``NDFrame.loc`` indexing when rows/columns were converted to Float64Index if target was an empty list/ndarray (:issue:`7774`)
- Bug in ``Series`` that allows it to be indexed by a ``DataFrame`` which has unexpected results. Such indexing is no longer permitted (:issue:`8444`)
- Bug in item assignment of a ``DataFrame`` with multi-index columns where right-hand-side columns were not aligned (:issue:`7655`)
- Bug in unpickling of categorical series and dataframe columns (:issue:`8518`)
15 changes: 15 additions & 0 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class Categorical(PandasObject):

# For comparisons, so that numpy uses our implementation if the compare ops, which raise
__array_priority__ = 1000
ordered = False
name = None

def __init__(self, values, categories=None, ordered=None, name=None, fastpath=False,
levels=None):
Expand Down Expand Up @@ -718,6 +720,19 @@ def __array__(self, dtype=None):
return np.asarray(ret, dtype)
return ret

def __setstate__(self, state):
"""Necessary for making this object picklable"""
if not isinstance(state, dict):
raise Exception('invalid pickle state')

if 'labels' in state:
state['_codes'] = state.pop('labels')
if '_levels' in state:
state['categories'] = state.pop('_levels')

for k, v in compat.iteritems(state):
setattr(self,k,v)

@property
def T(self):
return self
Expand Down
31 changes: 7 additions & 24 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,16 +1070,19 @@ class NonConsolidatableMixIn(object):
def __init__(self, values, placement,
ndim=None, fastpath=False,):

# Placement must be converted to BlockPlacement via property setter
# before ndim logic, because placement may be a slice which doesn't
# have a length.
self.mgr_locs = placement

# kludgetastic
if ndim is None:
if len(placement) != 1:
if len(self.mgr_locs) != 1:
ndim = 1
else:
ndim = 2
self.ndim = ndim

self.mgr_locs = placement

if not isinstance(values, self._holder):
raise TypeError("values must be {0}".format(self._holder.__name__))

Expand Down Expand Up @@ -1852,6 +1855,7 @@ def get_values(self, dtype=None):
.reshape(self.values.shape)
return self.values


class SparseBlock(NonConsolidatableMixIn, Block):
""" implement as a list of sparse arrays of the same dtype """
__slots__ = ()
Expand All @@ -1861,27 +1865,6 @@ class SparseBlock(NonConsolidatableMixIn, Block):
_ftype = 'sparse'
_holder = SparseArray

def __init__(self, values, placement,
ndim=None, fastpath=False,):

# Placement must be converted to BlockPlacement via property setter
# before ndim logic, because placement may be a slice which doesn't
# have a length.
self.mgr_locs = placement

# kludgetastic
if ndim is None:
if len(self.mgr_locs) != 1:
ndim = 1
else:
ndim = 2
self.ndim = ndim

if not isinstance(values, SparseArray):
raise TypeError("values must be SparseArray")

self.values = values

@property
def shape(self):
return (len(self.mgr_locs), self.sp_index.length)
Expand Down
Binary file not shown.
11 changes: 8 additions & 3 deletions pandas/io/tests/generate_legacy_pickles.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def create_data():
from pandas import (Series,TimeSeries,DataFrame,Panel,
SparseSeries,SparseTimeSeries,SparseDataFrame,SparsePanel,
Index,MultiIndex,PeriodIndex,
date_range,period_range,bdate_range,Timestamp)
date_range,period_range,bdate_range,Timestamp,Categorical)
nan = np.nan

data = {
Expand All @@ -85,7 +85,8 @@ def create_data():
mi = Series(np.arange(5).astype(np.float64),index=MultiIndex.from_tuples(tuple(zip(*[[1,1,2,2,2],
[3,4,3,4,5]])),
names=['one','two'])),
dup=Series(np.arange(5).astype(np.float64), index=['A', 'B', 'C', 'D', 'A']))
dup=Series(np.arange(5).astype(np.float64), index=['A', 'B', 'C', 'D', 'A']),
cat=Series(Categorical(['foo', 'bar', 'baz'])))

frame = dict(float = DataFrame(dict(A = series['float'], B = series['float'] + 1)),
int = DataFrame(dict(A = series['int'] , B = series['int'] + 1)),
Expand All @@ -95,7 +96,11 @@ def create_data():
['one','two','one','two','three']])),
names=['first','second'])),
dup=DataFrame(np.arange(15).reshape(5, 3).astype(np.float64),
columns=['A', 'B', 'A']))
columns=['A', 'B', 'A']),
cat_onecol=DataFrame(dict(A=Categorical(['foo', 'bar']))),
cat_and_float=DataFrame(dict(A=Categorical(['foo', 'bar', 'baz']),
B=np.arange(3))),
)
panel = dict(float = Panel(dict(ItemA = frame['float'], ItemB = frame['float']+1)),
dup = Panel(np.arange(30).reshape(3, 5, 2).astype(np.float64),
items=['A', 'B', 'A']))
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pandas.util.testing as tm
import pandas as pd
from pandas.util.testing import (
assert_almost_equal, assert_frame_equal, randn)
assert_almost_equal, assert_frame_equal, randn, assert_series_equal)
from pandas.compat import zip, u


Expand Down Expand Up @@ -363,6 +363,15 @@ def test_non_unique_pickle(self):
mgr2 = self.round_trip_pickle(mgr)
assert_frame_equal(DataFrame(mgr), DataFrame(mgr2))

def test_categorical_block_pickle(self):
mgr = create_mgr('a: category')
mgr2 = self.round_trip_pickle(mgr)
assert_frame_equal(DataFrame(mgr), DataFrame(mgr2))

smgr = create_single_mgr('category')
smgr2 = self.round_trip_pickle(smgr)
assert_series_equal(Series(smgr), Series(smgr2))

def test_get_scalar(self):
for item in self.mgr.items:
for i, index in enumerate(self.mgr.axes[1]):
Expand Down