Skip to content

Commit 0b2f1f4

Browse files
sinhrksjreback
authored andcommitted
CLN: Period cleanup related to array like meth
removed unused / duplicated internal method returning array-likes. Also added some tests to guarantee existing API before fixing its dtype (see #13941). Author: sinhrks <[email protected]> Closes #13955 from sinhrks/object_array_cln and squashes the following commits: a75a718 [sinhrks] CLN: Period cleanup related to array like meth
1 parent 7bfc7c4 commit 0b2f1f4

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

pandas/tseries/period.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,6 @@ def asof_locs(self, where, mask):
402402

403403
return result
404404

405-
def _array_values(self):
406-
return self.asobject
407-
408405
@Appender(_index_shared_docs['astype'])
409406
def astype(self, dtype, copy=True):
410407
dtype = np.dtype(dtype)
@@ -541,14 +538,9 @@ def start_time(self):
541538
def end_time(self):
542539
return self.to_timestamp(how='end')
543540

544-
def _get_object_array(self):
545-
freq = self.freq
546-
return np.array([Period._from_ordinal(ordinal=x, freq=freq)
547-
for x in self.values], copy=False)
548-
549541
def _mpl_repr(self):
550542
# how to represent ourselves to matplotlib
551-
return self._get_object_array()
543+
return self.asobject.values
552544

553545
def equals(self, other):
554546
"""

pandas/tseries/tests/test_period.py

+60-18
Original file line numberDiff line numberDiff line change
@@ -1783,24 +1783,6 @@ def test_constructor_datetime64arr(self):
17831783

17841784
self.assertRaises(ValueError, PeriodIndex, vals, freq='D')
17851785

1786-
def test_view(self):
1787-
idx = pd.PeriodIndex([], freq='M')
1788-
1789-
exp = np.array([], dtype=np.int64)
1790-
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1791-
tm.assert_numpy_array_equal(idx.asi8, exp)
1792-
1793-
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
1794-
1795-
exp = np.array([492, -9223372036854775808], dtype=np.int64)
1796-
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1797-
tm.assert_numpy_array_equal(idx.asi8, exp)
1798-
1799-
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
1800-
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
1801-
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1802-
tm.assert_numpy_array_equal(idx.asi8, exp)
1803-
18041786
def test_constructor_empty(self):
18051787
idx = pd.PeriodIndex([], freq='M')
18061788
tm.assertIsInstance(idx, PeriodIndex)
@@ -1988,6 +1970,66 @@ def test_constructor_freq_combined(self):
19881970
freq='25H')
19891971
tm.assert_index_equal(pidx, expected)
19901972

1973+
def test_view_asi8(self):
1974+
idx = pd.PeriodIndex([], freq='M')
1975+
1976+
exp = np.array([], dtype=np.int64)
1977+
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1978+
tm.assert_numpy_array_equal(idx.asi8, exp)
1979+
1980+
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
1981+
1982+
exp = np.array([492, -9223372036854775808], dtype=np.int64)
1983+
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1984+
tm.assert_numpy_array_equal(idx.asi8, exp)
1985+
1986+
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
1987+
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
1988+
tm.assert_numpy_array_equal(idx.view('i8'), exp)
1989+
tm.assert_numpy_array_equal(idx.asi8, exp)
1990+
1991+
def test_values(self):
1992+
# ToDo: .values and .get_values() should return Period as object
1993+
# dtype array. ._values shouldn't be changed
1994+
idx = pd.PeriodIndex([], freq='M')
1995+
1996+
exp = np.array([], dtype=np.int64)
1997+
tm.assert_numpy_array_equal(idx.values, exp)
1998+
tm.assert_numpy_array_equal(idx.get_values(), exp)
1999+
tm.assert_numpy_array_equal(idx._values, exp)
2000+
2001+
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
2002+
2003+
exp = np.array([492, -9223372036854775808], dtype=np.int64)
2004+
tm.assert_numpy_array_equal(idx.values, exp)
2005+
tm.assert_numpy_array_equal(idx.get_values(), exp)
2006+
tm.assert_numpy_array_equal(idx._values, exp)
2007+
2008+
exp = np.array([14975, -9223372036854775808], dtype=np.int64)
2009+
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
2010+
tm.assert_numpy_array_equal(idx.values, exp)
2011+
tm.assert_numpy_array_equal(idx.get_values(), exp)
2012+
tm.assert_numpy_array_equal(idx._values, exp)
2013+
2014+
def test_asobject_like(self):
2015+
idx = pd.PeriodIndex([], freq='M')
2016+
2017+
exp = np.array([], dtype=object)
2018+
tm.assert_numpy_array_equal(idx.asobject.values, exp)
2019+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
2020+
2021+
idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
2022+
2023+
exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object)
2024+
tm.assert_numpy_array_equal(idx.asobject.values, exp)
2025+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
2026+
2027+
exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT],
2028+
dtype=object)
2029+
idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
2030+
tm.assert_numpy_array_equal(idx.asobject.values, exp)
2031+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
2032+
19912033
def test_is_(self):
19922034
create_index = lambda: PeriodIndex(freq='A', start='1/1/2001',
19932035
end='12/1/2009')

0 commit comments

Comments
 (0)