def test_series_get_item(psr, arg): gsr = Series.from_pandas(psr) expect = psr[arg] got = gsr[arg] assert_eq(expect, got)
def test_series_loc_datetime(): if PANDAS_GE_110: kwargs = {"check_freq": False} else: kwargs = {} ps = pd.Series([1, 2, 3, 4, 5], index=pd.date_range("20010101", "20010105")) gs = Series.from_pandas(ps) # a few different ways of specifying a datetime label: assert_eq(ps.loc["20010101"], gs.loc["20010101"]) assert_eq(ps.loc["2001-01-01"], gs.loc["2001-01-01"]) assert_eq( ps.loc[pd.to_datetime("2001-01-01")], gs.loc[pd.to_datetime("2001-01-01")], ) assert_eq( ps.loc[np.datetime64("2001-01-01")], gs.loc[np.datetime64("2001-01-01")], ) assert_eq( ps.loc["2001-01-02":"2001-01-05"], gs.loc["2001-01-02":"2001-01-05"], **kwargs, ) assert_eq(ps.loc["2001-01-02":], gs.loc["2001-01-02":], **kwargs) assert_eq(ps.loc[:"2001-01-04"], gs.loc[:"2001-01-04"], **kwargs) assert_eq(ps.loc[::2], gs.loc[::2], **kwargs) assert_eq( ps.loc[["2001-01-01", "2001-01-04", "2001-01-05"]], gs.loc[["2001-01-01", "2001-01-04", "2001-01-05"]], ) assert_eq( ps.loc[[ pd.to_datetime("2001-01-01"), pd.to_datetime("2001-01-04"), pd.to_datetime("2001-01-05"), ]], gs.loc[[ pd.to_datetime("2001-01-01"), pd.to_datetime("2001-01-04"), pd.to_datetime("2001-01-05"), ]], ) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], **kwargs, ) just_less_than_max = ps.index.max() - pd.Timedelta("5m") assert_eq(ps.loc[:just_less_than_max], gs.loc[:just_less_than_max], **kwargs)
def test_series_take_positional(): psr = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"]) gsr = Series.from_pandas(psr) take_indices = [1, 2, 0, 3] expect = psr.take(take_indices) got = gsr.take(take_indices, keep_index=True) assert_eq(expect, got)
def test_series_loc_float_index(): ps = pd.Series([1, 2, 3, 4, 5], index=[5.43, 6.34, 7.34, 8.0, 9.1]) gs = Series.from_pandas(ps) assert_eq(ps.loc[5.43], gs.loc[5.43]) assert_eq(ps.loc[8], gs.loc[8]) assert_eq(ps.loc[6.1:8], gs.loc[6.1:8]) assert_eq(ps.loc[:7.1], gs.loc[:7.1]) assert_eq(ps.loc[6.345:], gs.loc[6.345:]) assert_eq(ps.loc[::2], gs.loc[::2]) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], )
def test_to_from_pandas_nulls(data, nulls): pd_data = pd.Series(data.copy().astype("datetime64[ns]")) if nulls == "some": # Fill half the values with NaT pd_data[list(range(0, len(pd_data), 2))] = np.datetime64("nat", "ns") elif nulls == "all": # Fill all the values with NaT pd_data[:] = np.datetime64("nat", "ns") gdf_data = Series.from_pandas(pd_data) expect = pd_data got = gdf_data.to_pandas() assert_eq(expect, got)
def test_series_loc_datetime(): ps = pd.Series( [1, 2, 3, 4, 5], index=pd.date_range("20010101", "20010105") ) gs = Series.from_pandas(ps) # a few different ways of specifying a datetime label: assert_eq(ps.loc["20010101"], gs.loc["20010101"]) assert_eq(ps.loc["2001-01-01"], gs.loc["2001-01-01"]) assert_eq( ps.loc[pd.to_datetime("2001-01-01")], gs.loc[pd.to_datetime("2001-01-01")], ) assert_eq( ps.loc[np.datetime64("2001-01-01")], gs.loc[np.datetime64("2001-01-01")], ) assert_eq( ps.loc["2001-01-02":"2001-01-05"], gs.loc["2001-01-02":"2001-01-05"] ) assert_eq(ps.loc["2001-01-02":], gs.loc["2001-01-02":]) assert_eq(ps.loc[:"2001-01-04"], gs.loc[:"2001-01-04"]) assert_eq(ps.loc[::2], gs.loc[::2]) # # assert_eq(ps.loc[['2001-01-01', '2001-01-04', '2001-01-05']], # gs.loc[['2001-01-01', '2001-01-04', '2001-01-05']]) # looks like a bug in Pandas doesn't let us check for the above, # so instead: assert_eq( ps.loc[ [ pd.to_datetime("2001-01-01"), pd.to_datetime("2001-01-04"), pd.to_datetime("2001-01-05"), ] ], gs.loc[ [ pd.to_datetime("2001-01-01"), pd.to_datetime("2001-01-04"), pd.to_datetime("2001-01-05"), ] ], ) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], )
def test_series_loc_numerical(): ps = pd.Series([1, 2, 3, 4, 5], index=[5, 6, 7, 8, 9]) gs = Series.from_pandas(ps) assert_eq(ps.loc[5], gs.loc[5]) assert_eq(ps.loc[6], gs.loc[6]) assert_eq(ps.loc[6:8], gs.loc[6:8]) assert_eq(ps.loc[:8], gs.loc[:8]) assert_eq(ps.loc[6:], gs.loc[6:]) assert_eq(ps.loc[::2], gs.loc[::2]) assert_eq(ps.loc[[5, 8, 9]], gs.loc[[5, 8, 9]]) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], )
def test_series_loc_string(): ps = pd.Series([1, 2, 3, 4, 5], index=["one", "two", "three", "four", "five"]) gs = Series.from_pandas(ps) assert_eq(ps.loc["one"], gs.loc["one"]) assert_eq(ps.loc["five"], gs.loc["five"]) assert_eq(ps.loc["two":"four"], gs.loc["two":"four"]) assert_eq(ps.loc[:"four"], gs.loc[:"four"]) assert_eq(ps.loc["two":], gs.loc["two":]) assert_eq(ps.loc[::2], gs.loc[::2]) assert_eq(ps.loc[["one", "four", "five"]], gs.loc[["one", "four", "five"]]) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], )
def test_series_loc_categorical(): ps = pd.Series([1, 2, 3, 4, 5], index=pd.Categorical(["a", "b", "c", "d", "e"])) gs = Series.from_pandas(ps) assert_eq(ps.loc["a"], gs.loc["a"]) assert_eq(ps.loc["e"], gs.loc["e"]) assert_eq(ps.loc["b":"d"], gs.loc["b":"d"]) assert_eq(ps.loc[:"d"], gs.loc[:"d"]) assert_eq(ps.loc["b":], gs.loc["b":]) assert_eq(ps.loc[::2], gs.loc[::2]) # order of categories changes, so we can only # compare values: assert_eq(ps.loc[["a", "d", "e"]].values, gs.loc[["a", "d", "e"]].to_array()) assert_eq( ps.loc[[True, False, True, False, True]], gs.loc[[True, False, True, False, True]], )