Skip to content

Commit 722e4ef

Browse files
committed
Disallow Series indexing by DataFrame
1 parent bca5699 commit 722e4ef

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/v0.15.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,4 @@ Bug Fixes
10261026
- Bug in assignment with indexer where type diversity would break alignment (:issue:`8258`)
10271027
- Bug in ``NDFrame.loc`` indexing when row/column names were lost when target was a list/ndarray (:issue:`6552`)
10281028
- Regression in ``NDFrame.loc`` indexing when rows/columns were converted to Float64Index if target was an empty list/ndarray (:issue:`7774`)
1029+
- Bug in Series that allows it to be indexed by a DataFrame which has unexpected results. Such indexing is no longer permitted (:issue:`8444`)

pandas/core/series.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
_possibly_convert_platform, _try_sort,
2222
ABCSparseArray, _maybe_match_name, _coerce_to_dtype,
2323
_ensure_object, SettingWithCopyError,
24-
_maybe_box_datetimelike)
24+
_maybe_box_datetimelike, ABCDataFrame)
2525
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
2626
_ensure_index)
2727
from pandas.core.indexing import _check_bool_indexer, _maybe_convert_indices
@@ -545,6 +545,9 @@ def _get_with(self, key):
545545
if isinstance(key, slice):
546546
indexer = self.index._convert_slice_indexer(key, typ='getitem')
547547
return self._get_values(indexer)
548+
elif isinstance(key, ABCDataFrame):
549+
raise TypeError('Indexing a Series with DataFrame is not supported, '\
550+
'use the appropriate DataFrame column')
548551
else:
549552
if isinstance(key, tuple):
550553
try:

pandas/tests/test_series.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,12 @@ def test_getitem_dups(self):
12221222
expected = Series([3,4],index=['C','C'],dtype=np.int64)
12231223
result = s['C']
12241224
assert_series_equal(result, expected)
1225+
1226+
def test_getitem_dataframe(self):
1227+
rng = list(range(10))
1228+
s = pd.Series(10, index=rng)
1229+
df = pd.DataFrame(rng, index=rng)
1230+
self.assertRaises(TypeError, s.__getitem__, df>5)
12251231

12261232
def test_setitem_ambiguous_keyerror(self):
12271233
s = Series(lrange(10), index=lrange(0, 20, 2))

0 commit comments

Comments
 (0)