Skip to content

Commit 111ff2b

Browse files
committed
Merge pull request #3887 from jreback/loc_name
BUG: (GH3880) index names are now propogated with loc/ix
2 parents 3d98544 + 3ca3222 commit 111ff2b

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

RELEASE.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ pandas 0.11.1
180180
- Non-unique indexing with a slice via ``loc`` and friends fixed (GH3659_)
181181
- Allow insert/delete to non-unique columns (GH3679_)
182182
- Extend ``reindex`` to correctly deal with non-unique indices (GH3679_)
183+
- ``DataFrame.itertuples()`` now works with frames with duplicate column
184+
names (GH3873_)
183185
- Fixed bug in groupby with empty series referencing a variable before assignment. (GH3510_)
184186
- Fixed bug in mixed-frame assignment with aligned series (GH3492_)
185187
- Fixed bug in selecting month/quarter/year from a series would not select the time element
@@ -231,8 +233,7 @@ pandas 0.11.1
231233
- PandasObjects raise TypeError when trying to hash (GH3882_)
232234
- Fix incorrect arguments passed to concat that are not list-like (e.g. concat(df1,df2)) (GH3481_)
233235
- Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) in ``read_csv`` (GH3795_)
234-
- ``DataFrame.itertuples()`` now works with frames with duplicate column
235-
names (GH3873_)
236+
- Fix index name not propogating when using ``loc/ix`` (GH3880_)
236237

237238
.. _GH3164: https://github.com/pydata/pandas/issues/3164
238239
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -327,6 +328,7 @@ pandas 0.11.1
327328
.. _GH3834: https://github.com/pydata/pandas/issues/3834
328329
.. _GH3873: https://github.com/pydata/pandas/issues/3873
329330
.. _GH3877: https://github.com/pydata/pandas/issues/3877
331+
.. _GH3880: https://github.com/pydata/pandas/issues/3880
330332

331333

332334
pandas 0.11.0

doc/source/v0.11.1.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,14 @@ Bug Fixes
348348
- Duplicate indexes with and empty DataFrame.from_records will return a correct frame (GH3562_)
349349
- Concat to produce a non-unique columns when duplicates are across dtypes is fixed (GH3602_)
350350
- Allow insert/delete to non-unique columns (GH3679_)
351+
- Non-unique indexing with a slice via ``loc`` and friends fixed (GH3659_)
352+
- Allow insert/delete to non-unique columns (GH3679_)
353+
- Extend ``reindex`` to correctly deal with non-unique indices (GH3679_)
354+
- ``DataFrame.itertuples()`` now works with frames with duplicate column
355+
names (GH3873_)
351356

352357
- ``DataFrame.from_records`` did not accept empty recarrays (GH3682_)
353358
- ``read_html`` now correctly skips tests (GH3741_)
354-
- ``DataFrame.itertuples()`` now works with frames with duplicate column
355-
names (GH3873_)
356359

357360
See the `full release notes
358361
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
@@ -405,3 +408,5 @@ on GitHub for a complete list.
405408
.. _GH3834: https://github.com/pydata/pandas/issues/3834
406409
.. _GH3873: https://github.com/pydata/pandas/issues/3873
407410
.. _GH3877: https://github.com/pydata/pandas/issues/3877
411+
.. _GH3659: https://github.com/pydata/pandas/issues/3659
412+
.. _GH3679: https://github.com/pydata/pandas/issues/3679

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,14 +2712,14 @@ def _reindex_multi(self, new_index, new_columns, copy, fill_value):
27122712
def _reindex_index(self, new_index, method, copy, level, fill_value=NA,
27132713
limit=None):
27142714
new_index, indexer = self.index.reindex(new_index, method, level,
2715-
limit=limit)
2715+
limit=limit, copy_if_needed=True)
27162716
return self._reindex_with_indexers(new_index, indexer, None, None,
27172717
copy, fill_value)
27182718

27192719
def _reindex_columns(self, new_columns, copy, level, fill_value=NA,
27202720
limit=None):
27212721
new_columns, indexer = self.columns.reindex(new_columns, level=level,
2722-
limit=limit)
2722+
limit=limit, copy_if_needed=True)
27232723
return self._reindex_with_indexers(None, None, new_columns, indexer,
27242724
copy, fill_value)
27252725

pandas/core/index.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def _get_method(self, method):
920920
}
921921
return aliases.get(method, method)
922922

923-
def reindex(self, target, method=None, level=None, limit=None):
923+
def reindex(self, target, method=None, level=None, limit=None, copy_if_needed=False):
924924
"""
925925
For Index, simply returns the new index and the results of
926926
get_indexer. Provided here to enable an interface that is amenable for
@@ -939,6 +939,12 @@ def reindex(self, target, method=None, level=None, limit=None):
939939
else:
940940
if self.equals(target):
941941
indexer = None
942+
943+
# to avoid aliasing an existing index
944+
if copy_if_needed and target.name != self.name and self.name is not None:
945+
if target.name is None:
946+
target = self.copy()
947+
942948
else:
943949
if self.is_unique:
944950
indexer = self.get_indexer(target, method=method,
@@ -2196,7 +2202,7 @@ def get_indexer(self, target, method=None, limit=None):
21962202

21972203
return com._ensure_platform_int(indexer)
21982204

2199-
def reindex(self, target, method=None, level=None, limit=None):
2205+
def reindex(self, target, method=None, level=None, limit=None, copy_if_needed=False):
22002206
"""
22012207
Performs any necessary conversion on the input index and calls
22022208
get_indexer. This method is here so MultiIndex and an Index of

pandas/core/internals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ def reindex_axis(self, new_axis, method=None, axis=0, copy=True):
19481948
'axis == 0')
19491949
return self.reindex_items(new_axis)
19501950

1951-
new_axis, indexer = cur_axis.reindex(new_axis, method)
1951+
new_axis, indexer = cur_axis.reindex(new_axis, method, copy_if_needed=True)
19521952
return self.reindex_indexer(new_axis, indexer, axis=axis)
19531953

19541954
def reindex_indexer(self, new_axis, indexer, axis=1, fill_value=np.nan):
@@ -2014,7 +2014,7 @@ def reindex_items(self, new_items, copy=True, fill_value=np.nan):
20142014
return data.reindex_items(new_items)
20152015

20162016
# TODO: this part could be faster (!)
2017-
new_items, indexer = self.items.reindex(new_items)
2017+
new_items, indexer = self.items.reindex(new_items, copy_if_needed=True)
20182018
new_axes = [new_items] + self.axes[1:]
20192019

20202020
# could have so me pathological (MultiIndex) issues here

pandas/tests/test_frame.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7207,6 +7207,7 @@ def test_reindex_name_remains(self):
72077207
s = Series(random.rand(10))
72087208
df = DataFrame(s, index=np.arange(len(s)))
72097209
i = Series(np.arange(10), name='iname')
7210+
72107211
df = df.reindex(i)
72117212
self.assert_(df.index.name == 'iname')
72127213

pandas/tests/test_indexing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,19 @@ def test_non_unique_loc(self):
10241024
expected = DataFrame({'A' : [2,4,5], 'B' : [4,6,7]}, index = [1,1,2])
10251025
assert_frame_equal(result,expected)
10261026

1027+
def test_loc_name(self):
1028+
# GH 3880
1029+
df = DataFrame([[1, 1], [1, 1]])
1030+
df.index.name = 'index_name'
1031+
result = df.iloc[[0, 1]].index.name
1032+
self.assert_(result == 'index_name')
1033+
1034+
result = df.ix[[0, 1]].index.name
1035+
self.assert_(result == 'index_name')
1036+
1037+
result = df.loc[[0, 1]].index.name
1038+
self.assert_(result == 'index_name')
1039+
10271040
if __name__ == '__main__':
10281041
import nose
10291042
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)