def test_astype(self): # GH 13149, GH 13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') result = idx.astype(object) expected = Index([Period('2016-05-16', freq='D')] + [Period(NaT, freq='D')] * 3, dtype='object') # Hack because of lack of support for Period null checking (GH12759) tm.assert_index_equal(result[:1], expected[:1]) result_arr = np.asarray([p.ordinal for p in result], dtype=np.int64) expected_arr = np.asarray([p.ordinal for p in expected], dtype=np.int64) tm.assert_numpy_array_equal(result_arr, expected_arr) # TODO: When GH12759 is resolved, change the above hack to: # tm.assert_index_equal(result, expected) # now, it raises. result = idx.astype(int) expected = Int64Index([16937] + [-9223372036854775808] * 3, dtype=np.int64) tm.assert_index_equal(result, expected) idx = period_range('1990', '2009', freq='A') result = idx.astype('i8') self.assert_index_equal(result, Index(idx.asi8)) self.assert_numpy_array_equal(result.values, idx.values)
def test_astype_conversion(self): # GH#13149, GH#13209 idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.NaN], freq="D", name="idx") result = idx.astype(object) expected = Index( [Period("2016-05-16", freq="D")] + [Period(NaT, freq="D")] * 3, dtype="object", name="idx", ) tm.assert_index_equal(result, expected) result = idx.astype(np.int64) expected = Int64Index([16937] + [-9223372036854775808] * 3, dtype=np.int64, name="idx") tm.assert_index_equal(result, expected) result = idx.astype(str) expected = Index([str(x) for x in idx], name="idx") tm.assert_index_equal(result, expected) idx = period_range("1990", "2009", freq="A", name="idx") result = idx.astype("i8") tm.assert_index_equal(result, Index(idx.asi8, name="idx")) tm.assert_numpy_array_equal(result.values, idx.asi8)
def test_period_astype_to_timestamp(self): pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS") res = pi.astype("datetime64[ns]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") res = pi.astype("datetime64[ns]", how="end") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") res = pi.astype("datetime64[ns, US/Eastern]", how="end") tm.assert_index_equal(res, exp) assert res.freq == exp.freq
def test_period_astype_to_timestamp(self): pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS") with tm.assert_produces_warning(FutureWarning): # how keyword deprecated GH#37982 res = pi.astype("datetime64[ns]", how="start") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") with tm.assert_produces_warning(FutureWarning): # how keyword deprecated GH#37982 res = pi.astype("datetime64[ns]", how="end") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") with tm.assert_produces_warning(FutureWarning): # how keyword deprecated GH#37982 res = pi.astype("datetime64[ns, US/Eastern]", how="end") tm.assert_index_equal(res, exp) assert res.freq == exp.freq
def test_astype_asfreq(self): pi1 = PeriodIndex(['2011-01-01', '2011-02-01', '2011-03-01'], freq='D') exp = PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='M') tm.assert_index_equal(pi1.asfreq('M'), exp) tm.assert_index_equal(pi1.astype('period[M]'), exp) exp = PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='3M') tm.assert_index_equal(pi1.asfreq('3M'), exp) tm.assert_index_equal(pi1.astype('period[3M]'), exp)
def test_astype_asfreq(self): pi1 = PeriodIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="D") exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") tm.assert_index_equal(pi1.asfreq("M"), exp) tm.assert_index_equal(pi1.astype("period[M]"), exp) exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="3M") tm.assert_index_equal(pi1.asfreq("3M"), exp) tm.assert_index_equal(pi1.astype("period[3M]"), exp)
def test_astype_object2(self): idx = pd.period_range(start='2013-01-01', periods=4, freq='M', name='idx') expected_list = [pd.Period('2013-01-31', freq='M'), pd.Period('2013-02-28', freq='M'), pd.Period('2013-03-31', freq='M'), pd.Period('2013-04-30', freq='M')] expected = pd.Index(expected_list, dtype=object, name='idx') result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object tm.assert_index_equal(result, expected) assert result.name == expected.name assert idx.tolist() == expected_list idx = PeriodIndex(['2013-01-01', '2013-01-02', 'NaT', '2013-01-04'], freq='D', name='idx') expected_list = [pd.Period('2013-01-01', freq='D'), pd.Period('2013-01-02', freq='D'), pd.Period('NaT', freq='D'), pd.Period('2013-01-04', freq='D')] expected = pd.Index(expected_list, dtype=object, name='idx') result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object tm.assert_index_equal(result, expected) for i in [0, 1, 3]: assert result[i] == expected[i] assert result[2] is pd.NaT assert result.name == expected.name result_list = idx.tolist() for i in [0, 1, 3]: assert result_list[i] == expected_list[i] assert result_list[2] is pd.NaT
def test_astype_object(self): idx = PeriodIndex([], freq="M") exp = np.array([], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) idx = PeriodIndex(["2011-01", NaT], freq="M") exp = np.array([Period("2011-01", freq="M"), NaT], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) exp = np.array([Period("2011-01-01", freq="D"), NaT], dtype=object) idx = PeriodIndex(["2011-01-01", NaT], freq="D") tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
def test_equals2(self, freq): # GH#13107 idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq) assert idx.equals(idx) assert idx.equals(idx.copy()) assert idx.equals(idx.astype(object)) assert idx.astype(object).equals(idx) assert idx.astype(object).equals(idx.astype(object)) assert not idx.equals(list(idx)) assert not idx.equals(pd.Series(idx)) idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="H") assert not idx.equals(idx2) assert not idx.equals(idx2.copy()) assert not idx.equals(idx2.astype(object)) assert not idx.astype(object).equals(idx2) assert not idx.equals(list(idx2)) assert not idx.equals(pd.Series(idx2)) # same internal, different tz idx3 = PeriodIndex._simple_new( idx._values._simple_new(idx._values.asi8, freq="H")) tm.assert_numpy_array_equal(idx.asi8, idx3.asi8) assert not idx.equals(idx3) assert not idx.equals(idx3.copy()) assert not idx.equals(idx3.astype(object)) assert not idx.astype(object).equals(idx3) assert not idx.equals(list(idx3)) assert not idx.equals(pd.Series(idx3))
def test_astype(self): # GH 13149, GH 13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') result = idx.astype(object) expected = Index([Period('2016-05-16', freq='D')] + [Period(NaT, freq='D')] * 3, dtype='object') tm.assert_index_equal(result, expected) result = idx.astype(int) expected = Int64Index([16937] + [-9223372036854775808] * 3, dtype=np.int64) tm.assert_index_equal(result, expected) idx = period_range('1990', '2009', freq='A') result = idx.astype('i8') tm.assert_index_equal(result, Index(idx.asi8)) tm.assert_numpy_array_equal(result.values, idx.asi8)
def test_astype_object2(self): idx = pd.period_range(start="2013-01-01", periods=4, freq="M", name="idx") expected_list = [ pd.Period("2013-01-31", freq="M"), pd.Period("2013-02-28", freq="M"), pd.Period("2013-03-31", freq="M"), pd.Period("2013-04-30", freq="M"), ] expected = pd.Index(expected_list, dtype=object, name="idx") result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object tm.assert_index_equal(result, expected) assert result.name == expected.name assert idx.tolist() == expected_list idx = PeriodIndex(["2013-01-01", "2013-01-02", "NaT", "2013-01-04"], freq="D", name="idx") expected_list = [ pd.Period("2013-01-01", freq="D"), pd.Period("2013-01-02", freq="D"), pd.Period("NaT", freq="D"), pd.Period("2013-01-04", freq="D"), ] expected = pd.Index(expected_list, dtype=object, name="idx") result = idx.astype(object) assert isinstance(result, Index) assert result.dtype == object tm.assert_index_equal(result, expected) for i in [0, 1, 3]: assert result[i] == expected[i] assert result[2] is pd.NaT assert result.name == expected.name result_list = idx.tolist() for i in [0, 1, 3]: assert result_list[i] == expected_list[i] assert result_list[2] is pd.NaT
def test_astype_raises(self, dtype): # GH#13149, GH#13209 idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.NaN], freq="D") msg = "Cannot cast PeriodArray to dtype" with pytest.raises(TypeError, match=msg): idx.astype(dtype)
def test_astype_raises(self, dtype): # GH#13149, GH#13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') msg = 'Cannot cast PeriodIndex to dtype' with tm.assert_raises_regex(TypeError, msg): idx.astype(dtype)
def test_astype_raises(self, dtype): # GH#13149, GH#13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') msg = 'Cannot cast PeriodArray to dtype' with pytest.raises(TypeError, match=msg): idx.astype(dtype)