Skip to content

Commit 76b2bf8

Browse files
committed
BUG: fix DatetimeIndex join with PeriodIndex stack overflow
1 parent 772ff01 commit 76b2bf8

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

pandas/tseries/period.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import pandas.tseries.frequencies as _freq_mod
1313

1414
import pandas.core.common as com
15-
from pandas.core.common import (isnull, _NS_DTYPE, _INT64_DTYPE,
16-
_maybe_box, _values_from_object)
15+
from pandas.core.common import (isnull, _INT64_DTYPE, _maybe_box,
16+
_values_from_object)
1717
from pandas import compat
1818
from pandas.lib import Timestamp
1919
import pandas.lib as lib
@@ -712,11 +712,9 @@ def _array_values(self):
712712
def astype(self, dtype):
713713
dtype = np.dtype(dtype)
714714
if dtype == np.object_:
715-
result = np.empty(len(self), dtype=dtype)
716-
result[:] = [x for x in self]
717-
return result
715+
return Index(np.array([x for x in self], dtype), dtype)
718716
elif dtype == _INT64_DTYPE:
719-
return self.values.copy()
717+
return Index(self.values.copy(), dtype)
720718
else: # pragma: no cover
721719
raise ValueError('Cannot cast PeriodIndex to dtype %s' % dtype)
722720

pandas/tseries/tests/test_period.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import pandas.core.datetools as datetools
2323
import pandas as pd
2424
import numpy as np
25-
from pandas.compat import range, lrange, lmap, map, zip
26-
randn = np.random.randn
25+
from numpy.random import randn
26+
from pandas.compat import range, lrange, lmap, zip
2727

2828
from pandas import Series, TimeSeries, DataFrame
2929
from pandas.util.testing import(assert_series_equal, assert_almost_equal,
@@ -1207,7 +1207,6 @@ def test_is_(self):
12071207
self.assertFalse(index.is_(index - 2))
12081208
self.assertFalse(index.is_(index - 0))
12091209

1210-
12111210
def test_comp_period(self):
12121211
idx = period_range('2007-01', periods=20, freq='M')
12131212

@@ -1913,6 +1912,17 @@ def test_join_self(self):
19131912
res = index.join(index, how=kind)
19141913
self.assert_(index is res)
19151914

1915+
def test_join_does_not_recur(self):
1916+
df = tm.makeCustomDataframe(3, 2, data_gen_f=lambda *args:
1917+
np.random.randint(2), c_idx_type='p',
1918+
r_idx_type='dt')
1919+
s = df.iloc[:2, 0]
1920+
1921+
res = s.index.join(df.columns, how='outer')
1922+
expected = Index([s.index[0], s.index[1],
1923+
df.columns[0], df.columns[1]], object)
1924+
np.testing.assert_array_equal(res, expected)
1925+
19161926
def test_align_series(self):
19171927
rng = period_range('1/1/2000', '1/1/2010', freq='A')
19181928
ts = Series(np.random.randn(len(rng)), index=rng)
@@ -2185,15 +2195,15 @@ def test_minutely(self):
21852195

21862196
def test_secondly(self):
21872197
self._check_freq('S', '1970-01-01')
2188-
2198+
21892199
def test_millisecondly(self):
21902200
self._check_freq('L', '1970-01-01')
21912201

21922202
def test_microsecondly(self):
21932203
self._check_freq('U', '1970-01-01')
2194-
2204+
21952205
def test_nanosecondly(self):
2196-
self._check_freq('N', '1970-01-01')
2206+
self._check_freq('N', '1970-01-01')
21972207

21982208
def _check_freq(self, freq, base_date):
21992209
rng = PeriodIndex(start=base_date, periods=10, freq=freq)

0 commit comments

Comments
 (0)