Skip to content

Commit 6ba858c

Browse files
stephenwlinchanghiskhan
authored andcommitted
BUG: Period slicing with period_range returns error
1 parent 1e2d8ec commit 6ba858c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def _get_with(self, key):
554554
key = list(key)
555555

556556
if isinstance(key, Index):
557-
key_type = lib.infer_dtype(key.values)
557+
key_type = key.inferred_type
558558
else:
559559
key_type = lib.infer_dtype(key)
560560

@@ -700,7 +700,7 @@ def _set_with(self, key, value):
700700
key = list(key)
701701

702702
if isinstance(key, Index):
703-
key_type = lib.infer_dtype(key.values)
703+
key_type = key.inferred_type
704704
else:
705705
key_type = lib.infer_dtype(key)
706706

pandas/tests/test_series.py

+32
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,38 @@ def test_asof(self):
25442544
d = self.ts.index[0] - datetools.bday
25452545
self.assert_(np.isnan(self.ts.asof(d)))
25462546

2547+
def test_getitem_setitem_periodindex(self):
2548+
from pandas import period_range, Period
2549+
# array or list or dates
2550+
N = 50
2551+
rng = period_range('1/1/1990', periods=N, freq='H')
2552+
ts = Series(np.random.randn(N), index=rng)
2553+
2554+
result = ts["1990-01-01 04":"1990-01-01 07"]
2555+
expected = ts[4:8]
2556+
assert_series_equal(result, expected)
2557+
2558+
result = ts.copy()
2559+
result["1990-01-01 04":"1990-01-01 07"] = 0
2560+
result["1990-01-01 04":"1990-01-01 07"] = ts[4:8]
2561+
assert_series_equal(result, ts)
2562+
2563+
lb = "1990-01-01 04"
2564+
rb = "1990-01-01 07"
2565+
result = ts[(ts.index >= lb) & (ts.index <= rb)]
2566+
expected = ts[4:8]
2567+
assert_series_equal(result, expected)
2568+
2569+
# GH 2782
2570+
result = ts[ts.index[4:8]]
2571+
expected = ts[4:8]
2572+
assert_series_equal(result, expected)
2573+
2574+
result = ts.copy()
2575+
result[ts.index[4:8]] = 0
2576+
result[4:8] = ts[4:8]
2577+
assert_series_equal(result, ts)
2578+
25472579
def test_asof_periodindex(self):
25482580
from pandas import period_range, PeriodIndex
25492581
# array or list or dates

0 commit comments

Comments
 (0)