Skip to content

Commit cbe2366

Browse files
committed
TST: add test for joining PeriodIndex with DatetimeIndex
1 parent 76b2bf8 commit cbe2366

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ Bug Fixes
553553
passed ``index_col=0`` (:issue:`5066`).
554554
- Fixed a bug where :func:`~pandas.read_html` was incorrectly infering the
555555
type of headers (:issue:`5048`).
556+
- Fixed a bug where ``DatetimeIndex`` joins with ``PeriodIndex`` caused a
557+
stack overflow (:issue:`3899`).
556558

557559

558560
pandas 0.12.0

pandas/tseries/period.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,10 @@ def _array_values(self):
712712
def astype(self, dtype):
713713
dtype = np.dtype(dtype)
714714
if dtype == np.object_:
715-
return Index(np.array([x for x in self], dtype), dtype)
715+
return Index(np.array(list(self), dtype), dtype)
716716
elif dtype == _INT64_DTYPE:
717717
return Index(self.values.copy(), dtype)
718-
else: # pragma: no cover
719-
raise ValueError('Cannot cast PeriodIndex to dtype %s' % dtype)
718+
raise ValueError('Cannot cast PeriodIndex to dtype %s' % dtype)
720719

721720
def __iter__(self):
722721
for val in self.values:

pandas/tseries/tests/test_period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ def test_join_does_not_recur(self):
19211921
res = s.index.join(df.columns, how='outer')
19221922
expected = Index([s.index[0], s.index[1],
19231923
df.columns[0], df.columns[1]], object)
1924-
np.testing.assert_array_equal(res, expected)
1924+
tm.assert_index_equal(res, expected)
19251925

19261926
def test_align_series(self):
19271927
rng = period_range('1/1/2000', '1/1/2010', freq='A')

pandas/tseries/tests/test_timeseries.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,19 @@ def test_ns_index(self):
20642064
new_index = pd.DatetimeIndex(start=index[0], end=index[-1], freq=index.freq)
20652065
self.assert_index_parameters(new_index)
20662066

2067+
def test_join_with_period_index(self):
2068+
df = tm.makeCustomDataframe(10, 10, data_gen_f=lambda *args:
2069+
np.random.randint(2), c_idx_type='p',
2070+
r_idx_type='dt')
2071+
s = df.iloc[:5, 0]
2072+
joins = 'left', 'right', 'inner', 'outer'
2073+
2074+
with tm.assertRaisesRegexp(ValueError,
2075+
'can only call with other PeriodIndex-ed '
2076+
'objects'):
2077+
for join in joins:
2078+
df.columns.join(s.index, how=join)
2079+
20672080

20682081
class TestDatetime64(unittest.TestCase):
20692082
"""

0 commit comments

Comments
 (0)