Skip to content

Commit 002da29

Browse files
jbrockmendeljreback
authored andcommitted
CLN: simplify pytables create_axes (#30068)
1 parent 4abc23e commit 002da29

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

pandas/io/pytables.py

+37-41
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,7 @@ def create_axes(
38323832
else:
38333833
existing_table = None
38343834

3835+
assert self.ndim == 2 # with next check, we must have len(axes) == 1
38353836
# currently support on ndim-1 axes
38363837
if len(axes) != self.ndim - 1:
38373838
raise ValueError(
@@ -3846,63 +3847,58 @@ def create_axes(
38463847
if nan_rep is None:
38473848
nan_rep = "nan"
38483849

3849-
# create axes to index and non_index
3850-
index_axes_map = dict()
3851-
for i, a in enumerate(obj.axes):
3850+
# We construct the non-index-axis first, since that alters self.info
3851+
idx = [x for x in [0, 1] if x not in axes][0]
38523852

3853-
if i in axes:
3854-
name = obj._AXIS_NAMES[i]
3855-
new_index = _convert_index(name, a, self.encoding, self.errors)
3856-
new_index.axis = i
3857-
index_axes_map[i] = new_index
3858-
3859-
else:
3853+
a = obj.axes[idx]
3854+
# we might be able to change the axes on the appending data if necessary
3855+
append_axis = list(a)
3856+
if existing_table is not None:
3857+
indexer = len(new_non_index_axes) # i.e. 0
3858+
exist_axis = existing_table.non_index_axes[indexer][1]
3859+
if not array_equivalent(np.array(append_axis), np.array(exist_axis)):
38603860

3861-
# we might be able to change the axes on the appending data if
3862-
# necessary
3863-
append_axis = list(a)
3864-
if existing_table is not None:
3865-
indexer = len(new_non_index_axes)
3866-
exist_axis = existing_table.non_index_axes[indexer][1]
3867-
if not array_equivalent(
3868-
np.array(append_axis), np.array(exist_axis)
3869-
):
3870-
3871-
# ahah! -> reindex
3872-
if array_equivalent(
3873-
np.array(sorted(append_axis)), np.array(sorted(exist_axis))
3874-
):
3875-
append_axis = exist_axis
3861+
# ahah! -> reindex
3862+
if array_equivalent(
3863+
np.array(sorted(append_axis)), np.array(sorted(exist_axis))
3864+
):
3865+
append_axis = exist_axis
38763866

3877-
# the non_index_axes info
3878-
info = _get_info(self.info, i)
3879-
info["names"] = list(a.names)
3880-
info["type"] = type(a).__name__
3867+
# the non_index_axes info
3868+
info = self.info.setdefault(idx, {})
3869+
info["names"] = list(a.names)
3870+
info["type"] = type(a).__name__
38813871

3882-
new_non_index_axes.append((i, append_axis))
3872+
new_non_index_axes.append((idx, append_axis))
38833873

3884-
self.non_index_axes = new_non_index_axes
3874+
# Now we can construct our new index axis
3875+
idx = axes[0]
3876+
a = obj.axes[idx]
3877+
name = obj._AXIS_NAMES[idx]
3878+
new_index = _convert_index(name, a, self.encoding, self.errors)
3879+
new_index.axis = idx
38853880

3886-
# set axis positions (based on the axes)
3887-
new_index_axes = [index_axes_map[a] for a in axes]
3888-
for j, iax in enumerate(new_index_axes):
3889-
iax.set_pos(j)
3890-
iax.update_info(self.info)
3881+
# Because we are always 2D, there is only one new_index, so
3882+
# we know it will have pos=0
3883+
new_index.set_pos(0)
3884+
new_index.update_info(self.info)
3885+
new_index.maybe_set_size(min_itemsize) # check for column conflicts
38913886

3892-
j = len(new_index_axes)
3887+
self.non_index_axes = new_non_index_axes
38933888

3894-
# check for column conflicts
3895-
for a in new_index_axes:
3896-
a.maybe_set_size(min_itemsize=min_itemsize)
3889+
new_index_axes = [new_index]
3890+
j = len(new_index_axes) # i.e. 1
3891+
assert j == 1
38973892

38983893
# reindex by our non_index_axes & compute data_columns
3894+
assert len(new_non_index_axes) == 1
38993895
for a in new_non_index_axes:
39003896
obj = _reindex_axis(obj, a[0], a[1])
39013897

39023898
def get_blk_items(mgr, blocks):
39033899
return [mgr.items.take(blk.mgr_locs) for blk in blocks]
39043900

3905-
transposed = new_index_axes[0].axis == 1
3901+
transposed = new_index.axis == 1
39063902

39073903
# figure out data_columns and get out blocks
39083904
block_obj = self.get_object(obj, transposed)._consolidate()

0 commit comments

Comments
 (0)