Skip to content

CLN: pytables create_axes cleanup #30127

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

Merged
merged 2 commits into from
Dec 8, 2019
Merged
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
46 changes: 16 additions & 30 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,7 @@ def update_info(self, info):
for key in self._info_fields:

value = getattr(self, key, None)
idx = _get_info(info, self.name)
idx = info.setdefault(self.name, {})

existing_value = idx.get(key)
if key in idx and value is not None and existing_value != value:
Expand Down Expand Up @@ -3757,7 +3757,6 @@ def create_axes(

# create according to the new data
new_non_index_axes: List = []
new_data_columns: List[Optional[str]] = []

# nan_representation
if nan_rep is None:
Expand Down Expand Up @@ -3800,8 +3799,6 @@ def create_axes(
new_index.update_info(self.info)
new_index.maybe_set_size(min_itemsize) # check for column conflicts

self.non_index_axes = new_non_index_axes

new_index_axes = [new_index]
j = len(new_index_axes) # i.e. 1
assert j == 1
Expand All @@ -3820,22 +3817,21 @@ def get_blk_items(mgr, blocks):
block_obj = self.get_object(obj, transposed)._consolidate()
blocks = block_obj._data.blocks
blk_items = get_blk_items(block_obj._data, blocks)
if len(new_non_index_axes):

data_columns = self.validate_data_columns(
data_columns, min_itemsize, new_non_index_axes
)
if len(data_columns):
axis, axis_labels = new_non_index_axes[0]
data_columns = self.validate_data_columns(
data_columns, min_itemsize, new_non_index_axes
)
if len(data_columns):
mgr = block_obj.reindex(
Index(axis_labels).difference(Index(data_columns)), axis=axis
)._data

blocks = list(mgr.blocks)
blk_items = get_blk_items(mgr, blocks)
for c in data_columns:
mgr = block_obj.reindex([c], axis=axis)._data
blocks.extend(mgr.blocks)
blk_items.extend(get_blk_items(mgr, mgr.blocks))
new_labels = Index(axis_labels).difference(Index(data_columns))
mgr = block_obj.reindex(new_labels, axis=axis)._data

blocks = list(mgr.blocks)
blk_items = get_blk_items(mgr, blocks)
for c in data_columns:
mgr = block_obj.reindex([c], axis=axis)._data
blocks.extend(mgr.blocks)
blk_items.extend(get_blk_items(mgr, mgr.blocks))

# reorder the blocks in the same order as the existing_table if we can
if existing_table is not None:
Expand Down Expand Up @@ -3875,7 +3871,6 @@ def get_blk_items(mgr, blocks):
if not (name is None or isinstance(name, str)):
# TODO: should the message here be more specifically non-str?
raise ValueError("cannot have non-object label DataIndexableCol")
new_data_columns.append(name)

# make sure that we match up the existing columns
# if we have an existing table
Expand Down Expand Up @@ -3916,7 +3911,7 @@ def get_blk_items(mgr, blocks):
j += 1

self.nan_rep = nan_rep
self.data_columns = new_data_columns
self.data_columns = [col.name for col in vaxes if col.is_data_indexable]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think/hope that something like this is the general case and data_columns can become a property

self.values_axes = vaxes
self.index_axes = new_index_axes
self.non_index_axes = new_non_index_axes
Expand Down Expand Up @@ -4605,15 +4600,6 @@ def _reindex_axis(obj, axis: int, labels: Index, other=None):
return obj


def _get_info(info, name):
""" get/create the info for this name """
try:
idx = info[name]
except KeyError:
idx = info[name] = dict()
return idx


# tz to/from coercion


Expand Down