Skip to content

Commit 87eafb4

Browse files
committed
fix some tests
1 parent 291ac70 commit 87eafb4

File tree

8 files changed

+98
-65
lines changed

8 files changed

+98
-65
lines changed

pandas/core/apply.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ def apply(self) -> DataFrame | Series:
726726
with np.errstate(all="ignore"):
727727
results = self.obj._mgr.apply("apply", func=self.f)
728728
# _constructor will retain self.index and self.columns
729-
return self.obj._constructor(data=results)
729+
axes_dict = self.obj._construct_axes_dict()
730+
return self.obj._constructor(data=results, **axes_dict)
730731

731732
# broadcasting
732733
if self.result_type == "broadcast":

pandas/core/arraylike.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ def _reconstruct(result):
359359
if isinstance(result, BlockManager):
360360
# we went through BlockManager.apply e.g. np.sqrt
361361
# TODO: any cases that aren't index/columns-preserving?
362-
if self.ndim == 1:
363-
reconstruct_kwargs["index"] = self.index
364-
else:
365-
reconstruct_kwargs["index"] = self.index
366-
reconstruct_kwargs["columns"] = self.columns
362+
reconstruct_kwargs.update(self._construct_axes_dict())
367363
result = self._constructor(result, **reconstruct_kwargs, copy=False)
368364
else:
369365
# we converted an array, lost our axes

pandas/core/frame.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,9 @@ def __init__(
621621
dtype = self._validate_dtype(dtype)
622622

623623
if isinstance(data, DataFrame):
624-
if index is None and columns is None:
624+
if index is None:
625625
index = data.index
626+
if columns is None:
626627
columns = data.columns
627628
data = data._mgr
628629

@@ -631,10 +632,14 @@ def __init__(
631632
# -> use fastpath (without checking Manager type)
632633
if index is None or columns is None:
633634
assert False
634-
if not index.equals(data.axes[-1]):#index is not data.axes[-1]:
635-
assert False
636-
if not columns.equals(data.axes[0]):#columns is not data.axes[0]:
637-
assert False
635+
if data.axes[0] is not columns or data.axes[1] is not index:
636+
# FIXME: without this check, json tests segfault...
637+
# nope, segfaults even with this check
638+
data.axes = [ensure_index(columns), ensure_index(index)]
639+
#if not index.equals(data.axes[-1]):#index is not data.axes[-1]:
640+
# assert False
641+
#if not columns.equals(data.axes[0]):#columns is not data.axes[0]:
642+
# assert False
638643
if index is None and columns is None and dtype is None and not copy:
639644
# GH#33357 fastpath
640645
NDFrame.__init__(self, data)
@@ -2410,7 +2415,6 @@ def maybe_reorder(
24102415
manager = get_option("mode.data_manager")
24112416
mgr, index, columns = arrays_to_mgr(arrays, columns, result_index, typ=manager)
24122417

2413-
# FIXME: get axes without mgr.axes
24142418
return cls(mgr, index=index, columns=columns)
24152419

24162420
def to_records(
@@ -4164,6 +4168,7 @@ def _set_item_mgr(self, key, value: ArrayLike) -> None:
41644168
except KeyError:
41654169
# This item wasn't present, just insert at end
41664170
self._mgr.insert(len(self._info_axis), key, value)
4171+
self._columns = self.columns.insert(len(self._info_axis), key)
41674172
else:
41684173
self._iset_item_mgr(loc, value)
41694174

@@ -4765,7 +4770,9 @@ def predicate(arr: ArrayLike) -> bool:
47654770
return True
47664771

47674772
mgr = self._mgr._get_data_subset(predicate).copy(deep=None)
4768-
return type(self)(mgr).__finalize__(self)
4773+
# FIXME: get axes without mgr.axes
4774+
assert mgr.axes[1] is self.index # WTF why does passing columns/index cause segfault?
4775+
return type(self)(mgr, columns=mgr.axes[0], index=mgr.axes[1]).__finalize__(self)
47694776

47704777
def insert(
47714778
self,
@@ -5865,7 +5872,7 @@ def shift(
58655872
fill_value=fill_value,
58665873
allow_dups=True,
58675874
)
5868-
res_df = self._constructor(mgr)
5875+
res_df = self._constructor(mgr, columns=self.columns, index=self.index)
58695876
return res_df.__finalize__(self, method="shift")
58705877

58715878
return super().shift(
@@ -6392,7 +6399,8 @@ class max type
63926399

63936400
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
63946401
def isna(self) -> DataFrame:
6395-
result = self._constructor(self._mgr.isna(func=isna))
6402+
axes_dict = self._construct_axes_dict()
6403+
result = self._constructor(self._mgr.isna(func=isna), **axes_dict)
63966404
return result.__finalize__(self, method="isna")
63976405

63986406
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
@@ -6944,19 +6952,26 @@ def sort_values( # type: ignore[override]
69446952
else:
69456953
return self.copy()
69466954

6955+
bm_axis = self._get_block_manager_axis(axis)
6956+
69476957
new_data = self._mgr.take(
6948-
indexer, axis=self._get_block_manager_axis(axis), verify=False
6958+
indexer, axis=bm_axis, verify=False
69496959
)
69506960

6951-
if ignore_index:
6952-
new_data.set_axis(
6953-
self._get_block_manager_axis(axis), default_index(len(indexer))
6954-
)
6955-
# FIXME: get axes without mgr.axes
6961+
axis_name = self._get_axis_name(axis)
6962+
69566963
axes_dict = {}
6957-
axes_dict["index"] = new_data.axes[-1]
6958-
if self.ndim == 2:
6959-
axes_dict["columns"] = new_data.axes[0]
6964+
axes_dict[axis_name] = self.axes[axis].take(indexer)
6965+
if axis == 0:
6966+
axes_dict["columns"] = self.columns
6967+
else:
6968+
axes_dict["index"] = self.index
6969+
6970+
if ignore_index:
6971+
rng = default_index(len(indexer))
6972+
new_data.set_axis(bm_axis, rng)
6973+
axes_dict[axis_name] = rng
6974+
69606975
result = self._constructor(new_data, **axes_dict)
69616976
if inplace:
69626977
return self._update_inplace(result)
@@ -10913,9 +10928,12 @@ def _get_data() -> DataFrame:
1091310928

1091410929
# After possibly _get_data and transposing, we are now in the
1091510930
# simple case where we can use BlockManager.reduce
10916-
res, _ = df._mgr.reduce(blk_func, ignore_failures=ignore_failures)
10917-
# FIXME: get axes without mgr.axes
10918-
out = df._constructor(res, index=res.axes[1], columns=res.axes[0]).iloc[0]
10931+
res, indexer = df._mgr.reduce(blk_func, ignore_failures=ignore_failures)
10932+
index = Index([None], dtype=object)
10933+
assert index.equals(res.axes[1])
10934+
columns = self.columns.take(indexer)
10935+
assert columns.equals(res.axes[0])
10936+
out = df._constructor(res, index=index, columns=columns).iloc[0]
1091910937
if out_dtype is not None:
1092010938
out = out.astype(out_dtype)
1092110939
if axis == 0 and len(self) == 0 and name in ["sum", "prod"]:
@@ -11413,7 +11431,8 @@ def quantile(
1141311431
res = data._mgr.take(indexer[q_idx], verify=False)
1141411432
res.axes[1] = q
1141511433

11416-
result = self._constructor(res)
11434+
# FIXME: get axes without mgr.axes
11435+
result = self._constructor(res, columns=res.axes[0], index=res.axes[1])
1141711436
return result.__finalize__(self, method="quantile")
1141811437

1141911438
@doc(NDFrame.asfreq, **_shared_doc_kwargs)

pandas/core/generic.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3924,7 +3924,7 @@ def _take(
39243924
convert_indices=convert_indices,
39253925
)
39263926
axes_dict = self._construct_axes_dict()
3927-
#axes_dict[axis] = self.axes[axis].take(indices) # FIXME: get axes without mgr.axes
3927+
# FIXME: get axes without mgr.axes
39283928
axes_dict[self._get_axis_name(axis)] = new_data.axes[self._get_block_manager_axis(axis)]
39293929
return self._constructor(new_data, **axes_dict).__finalize__(self, method="take")
39303930

@@ -4113,7 +4113,7 @@ class animal locomotion
41134113
new_mgr = self._mgr.fast_xs(loc)
41144114

41154115
result = self._constructor_sliced(
4116-
new_mgr, name=self.index[loc]
4116+
new_mgr, name=self.index[loc], index=self.columns
41174117
).__finalize__(self)
41184118
elif is_scalar(loc):
41194119
result = self.iloc[:, slice(loc, loc + 1)]
@@ -4156,6 +4156,14 @@ def _slice(self: NDFrameT, slobj: slice, axis=0) -> NDFrameT:
41564156
result._set_is_copy(self, copy=is_copy)
41574157
return result
41584158

4159+
@staticmethod
4160+
def _get_axes_from_mgr(mgr):
4161+
axes_dict = {}
4162+
axes_dict["index"] = mgr.axes[-1]
4163+
if mgr.ndim == 2:
4164+
axes_dict["columns"] = mgr.axes[0]
4165+
return axes_dict
4166+
41594167
@final
41604168
def _set_is_copy(self, ref: NDFrame, copy: bool_t = True) -> None:
41614169
if not copy:
@@ -4291,6 +4299,11 @@ def __delitem__(self, key) -> None:
42914299
# exception:
42924300
loc = self.axes[-1].get_loc(key)
42934301
self._mgr = self._mgr.idelete(loc)
4302+
# FIXME: get axes without mgr.axes
4303+
if self.ndim == 1:
4304+
self._index = self._mgr.axes[0]
4305+
else:
4306+
self._columns = self._mgr.axes[0]
42944307

42954308
# delete from the caches
42964309
try:
@@ -4639,10 +4652,7 @@ def _drop_axis(
46394652
only_slice=only_slice,
46404653
)
46414654
# FIXME: get axes without mgr.axes
4642-
axes_dict = {}
4643-
axes_dict["index"] = new_mgr.axes[-1]
4644-
if self.ndim == 2:
4645-
axes_dict["columns"] = new_mgr.axes[0]
4655+
axes_dict = self._get_axes_from_mgr(new_mgr)
46464656
result = self._constructor(new_mgr, **axes_dict)
46474657
if self.ndim == 1:
46484658
result.name = self.name
@@ -5104,11 +5114,8 @@ def sort_index(
51045114
axis = 1 if isinstance(self, ABCDataFrame) else 0
51055115
new_data.set_axis(axis, default_index(len(indexer)))
51065116

5107-
axes_dict = {}#self._construct_axes_dict()
51085117
# FIXME: get axes without mgr.axes
5109-
axes_dict["index"] = new_data.axes[-1]
5110-
if self.ndim == 2:
5111-
axes_dict["columns"] = new_data.axes[0]
5118+
axes_dict = self._get_axes_from_mgr(new_data)
51125119
result = self._constructor(new_data, **axes_dict)
51135120

51145121
if inplace:
@@ -5447,10 +5454,7 @@ def _reindex_with_indexers(
54475454
new_data = new_data.copy()
54485455

54495456
# FIXME: get axes without mgr.axes
5450-
if self.ndim == 1:
5451-
axes_dict = {"index": new_data.axes[0]}
5452-
else:
5453-
axes_dict = {"index": new_data.axes[1], "columns": new_data.axes[0]}
5457+
axes_dict = self._get_axes_from_mgr(new_data)
54545458

54555459
return self._constructor(new_data, **axes_dict).__finalize__(self)
54565460

@@ -6110,22 +6114,16 @@ def _check_inplace_setting(self, value) -> bool_t:
61106114

61116115
@final
61126116
def _get_numeric_data(self: NDFrameT) -> NDFrameT:
6113-
# FIXME: get axes without mgr.axes
61146117
mgr = self._mgr.get_numeric_data()
6115-
axes_dict = {}
6116-
axes_dict["index"] = mgr.axes[-1]
6117-
if self.ndim == 2:
6118-
axes_dict["columns"] = mgr.axes[0]
6118+
# FIXME: get axes without mgr.axes
6119+
axes_dict = self._get_axes_from_mgr(mgr)
61196120
return self._constructor(mgr, **axes_dict).__finalize__(self)
61206121

61216122
@final
61226123
def _get_bool_data(self):
6123-
# FIXME: get axes without mgr.axes
61246124
mgr = self._mgr.get_bool_data()
6125-
axes_dict = {}
6126-
axes_dict["index"] = mgr.axes[-1]
6127-
if self.ndim == 2:
6128-
axes_dict["columns"] = mgr.axes[0]
6125+
# FIXME: get axes without mgr.axes
6126+
axes_dict = self._get_axes_from_mgr(mgr)
61296127
return self._constructor(mgr, **axes_dict).__finalize__(self)
61306128

61316129
# ----------------------------------------------------------------------
@@ -6563,8 +6561,10 @@ def infer_objects(self: NDFrameT) -> NDFrameT:
65636561
# numeric=False necessary to only soft convert;
65646562
# python objects will still be converted to
65656563
# native numpy numeric types
6564+
axes_dict = self._construct_axes_dict()
65666565
return self._constructor(
6567-
self._mgr.convert(datetime=True, numeric=False, timedelta=True, copy=True)
6566+
self._mgr.convert(datetime=True, numeric=False, timedelta=True, copy=True),
6567+
**axes_dict,
65686568
).__finalize__(self, method="infer_objects")
65696569

65706570
@final
@@ -9693,10 +9693,7 @@ def _align_series(
96939693
fdata = fdata.copy()
96949694

96959695
# FIXME: get axes without mgr.axes
9696-
if self.ndim == 1:
9697-
axes_dict = {"index": fdata.axes[0]}
9698-
else:
9699-
axes_dict = {"index": fdata.axes[1], "columns": fdata.axes[0]}
9696+
axes_dict = self._get_axes_from_mgr(fdata)
97009697
left = self._constructor(fdata, **axes_dict)
97019698

97029699
if ridx is None:
@@ -10282,7 +10279,8 @@ def shift(
1028210279
new_data = self._mgr.shift(
1028310280
periods=periods, axis=axis, fill_value=fill_value
1028410281
)
10285-
return self._constructor(new_data).__finalize__(self, method="shift")
10282+
axes_dict = self._construct_axes_dict()
10283+
return self._constructor(new_data, **axes_dict).__finalize__(self, method="shift")
1028610284

1028710285
# when freq is given, index is shifted, data is not
1028810286
index = self._get_axis(axis)

pandas/core/groupby/generic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame:
11601160
for i, (item, sgb) in enumerate(self._iterate_column_groupbys(obj)):
11611161
result[i] = sgb.aggregate(func, *args, **kwargs)
11621162

1163-
res_df = self.obj._constructor(result)
1163+
res_df = self.obj._constructor(result, columns=obj.columns)
11641164
res_df.columns = obj.columns
11651165
return res_df
11661166

@@ -1335,7 +1335,8 @@ def arr_func(bvalues: ArrayLike) -> ArrayLike:
13351335
if len(res_mgr) < orig_mgr_len:
13361336
warn_dropping_nuisance_columns_deprecated(type(self), how, numeric_only)
13371337

1338-
res_df = self.obj._constructor(res_mgr)
1338+
# FIXME: get axes without mgr.axes
1339+
res_df = self.obj._constructor(res_mgr, index=res_mgr.axes[1], columns=res_mgr.axes[0])
13391340
if self.axis == 1:
13401341
res_df = res_df.T
13411342
return res_df
@@ -1657,15 +1658,15 @@ def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
16571658
index = Index(range(rows))
16581659
mgr.set_axis(1, index)
16591660
# FIXME: get axes without mgr.axes
1660-
result = self.obj._constructor(mgr, index=mgr.axes[1], columns=mgr.axes[0])
1661+
result = self.obj._constructor(mgr, index=index, columns=mgr.axes[0])
16611662

16621663
self._insert_inaxis_grouper_inplace(result)
16631664
result = result._consolidate()
16641665
else:
16651666
index = self.grouper.result_index
16661667
mgr.set_axis(1, index)
16671668
# FIXME: get axes without mgr.axes
1668-
result = self.obj._constructor(mgr, index=mgr.axes[1], columns=mgr.axes[0])
1669+
result = self.obj._constructor(mgr, index=index, columns=mgr.axes[0])
16691670

16701671
if self.axis == 1:
16711672
result = result.T

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2906,7 +2906,8 @@ def blk_func(values: ArrayLike) -> ArrayLike:
29062906
mgr = obj._mgr
29072907
res_mgr = mgr.apply(blk_func)
29082908

2909-
new_obj = obj._constructor(res_mgr)
2909+
axes_dict = obj._construct_axes_dict()
2910+
new_obj = obj._constructor(res_mgr, **axes_dict)
29102911
if isinstance(new_obj, Series):
29112912
new_obj.name = obj.name
29122913

0 commit comments

Comments
 (0)