Skip to content

Commit 47785e3

Browse files
committed
Merge pull request #6525 from immerrr/fix-df-column-slice-indexer
BUG: fix DataFarame column slice indexer
2 parents 30d3cba + a1a2cfb commit 47785e3

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Bug Fixes
194194
- Bug in ``read_html`` tests where redirected invalid URLs would make one test
195195
fail (:issue:`6445`).
196196
- Bug in multi-axis indexing using ``.loc`` on non-unique indices (:issue:`6504`)
197+
- Bug that caused _ref_locs corruption when slice indexing across columns axis of a DataFrame (:issue:`6525`)
197198

198199
pandas 0.13.1
199200
-------------

pandas/core/internals.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def take_ref_locs(self, indexer):
133133
tindexer[indexer] = False
134134
tindexer = tindexer.astype(int).cumsum()[indexer]
135135
ref_locs = ref_locs[indexer]
136+
137+
# Make sure the result is a copy, or otherwise self._ref_locs will be
138+
# updated.
139+
if ref_locs.base is not None:
140+
ref_locs = ref_locs.copy()
141+
136142
ref_locs -= tindexer
137143
return ref_locs
138144

pandas/tests/test_frame.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12267,6 +12267,19 @@ def test_empty_frame_dtypes_ftypes(self):
1226712267
('b', 'bool:dense'),
1226812268
('c', 'float64:dense')])))
1226912269

12270+
def test_dtypes_are_correct_after_column_slice(self):
12271+
# GH6525
12272+
df = pd.DataFrame(index=range(5), columns=list("abc"), dtype=np.float_)
12273+
odict = OrderedDict
12274+
assert_series_equal(df.dtypes,
12275+
pd.Series(odict([('a', np.float_), ('b', np.float_),
12276+
('c', np.float_),])))
12277+
assert_series_equal(df.iloc[:,2:].dtypes,
12278+
pd.Series(odict([('c', np.float_)])))
12279+
assert_series_equal(df.dtypes,
12280+
pd.Series(odict([('a', np.float_), ('b', np.float_),
12281+
('c', np.float_),])))
12282+
1227012283

1227112284
def skip_if_no_ne(engine='numexpr'):
1227212285
if engine == 'numexpr':

0 commit comments

Comments
 (0)