|
45 | 45 | ) |
46 | 46 | import xarray as xr |
47 | 47 |
|
| 48 | +from pandas._libs.tslibs.offsets import Day |
48 | 49 | from pandas._typing import ( |
49 | 50 | DtypeObj, |
50 | 51 | Scalar, |
@@ -1980,16 +1981,92 @@ def test_dtype_type() -> None: |
1980 | 1981 |
|
1981 | 1982 | def test_types_to_numpy() -> None: |
1982 | 1983 | s = pd.Series(["a", "b", "c"], dtype=str) |
1983 | | - check(assert_type(s.to_numpy(), np_1darray), np_1darray) |
1984 | | - check(assert_type(s.to_numpy(dtype="str", copy=True), np_1darray), np_1darray) |
1985 | | - check(assert_type(s.to_numpy(na_value=0), np_1darray), np_1darray) |
1986 | | - check(assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray), np_1darray) |
1987 | | - check(assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray), np_1darray) |
1988 | | - check(assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray), np_1darray) |
| 1984 | + check(assert_type(s.to_numpy(), np_1darray[np.str_]), np_1darray) |
| 1985 | + check( |
| 1986 | + assert_type(s.to_numpy(dtype="str", copy=True), np_1darray[np.str_]), np_1darray |
| 1987 | + ) |
| 1988 | + check(assert_type(s.to_numpy(na_value=0), np_1darray[np.str_]), np_1darray) |
| 1989 | + check( |
| 1990 | + assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray[np.str_]), np_1darray |
| 1991 | + ) |
| 1992 | + check( |
| 1993 | + assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray[np.str_]), np_1darray |
| 1994 | + ) |
| 1995 | + check( |
| 1996 | + assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray[np.str_]), |
| 1997 | + np_1darray, |
| 1998 | + ) |
1989 | 1999 |
|
1990 | 2000 | check(assert_type(pd.Series().to_numpy(), np_1darray), np_1darray) |
1991 | 2001 |
|
1992 | 2002 |
|
| 2003 | +def test_to_numpy() -> None: |
| 2004 | + """Test Series.to_numpy for different types.""" |
| 2005 | + s1 = pd.Series(["a", "b", "c"], dtype=str) |
| 2006 | + check(assert_type(s1.to_numpy(), np_1darray[np.str_]), np_1darray, str) |
| 2007 | + |
| 2008 | + s2 = pd.Series(["a", "b", "c"]).astype(bytes) |
| 2009 | + check(assert_type(s2.to_numpy(), np_1darray[np.bytes_]), np_1darray, np.bytes_) |
| 2010 | + |
| 2011 | + s3 = pd.Series([True, False]) |
| 2012 | + check(assert_type(s3.to_numpy(), np_1darray[np.bool_]), np_1darray, np.bool_) |
| 2013 | + |
| 2014 | + s4 = pd.Series([2, 3, 4]) |
| 2015 | + check(assert_type(s4.to_numpy(), np_1darray[np.integer]), np_1darray, np.integer) |
| 2016 | + |
| 2017 | + s5 = pd.Series([2.0, 3.54, 4.84]) |
| 2018 | + check(assert_type(s5.to_numpy(), np_1darray[np.floating]), np_1darray, np.floating) |
| 2019 | + |
| 2020 | + s6 = pd.Series([2.0 + 2j, 3.54 + 4j, 4.84]) |
| 2021 | + check( |
| 2022 | + assert_type(s6.to_numpy(), np_1darray[np.complexfloating]), |
| 2023 | + np_1darray, |
| 2024 | + np.complexfloating, |
| 2025 | + ) |
| 2026 | + |
| 2027 | + dates = pd.Series( |
| 2028 | + [ |
| 2029 | + pd.Timestamp("2020-01-01"), |
| 2030 | + pd.Timestamp("2020-01-15"), |
| 2031 | + pd.Timestamp("2020-02-01"), |
| 2032 | + ], |
| 2033 | + dtype="datetime64[ns]", |
| 2034 | + ) |
| 2035 | + s7 = pd.Series(pd.PeriodIndex(dates, freq="M")) |
| 2036 | + check(assert_type(s7.to_numpy(), np_1darray[np.object_]), np_1darray, pd.Period) |
| 2037 | + |
| 2038 | + s8 = pd.Series( |
| 2039 | + [ |
| 2040 | + pd.Interval(date, date + pd.DateOffset(days=1), closed="left") |
| 2041 | + for date in dates |
| 2042 | + ] |
| 2043 | + ) |
| 2044 | + check(assert_type(s8.to_numpy(), np_1darray[np.object_]), np_1darray, pd.Interval) |
| 2045 | + |
| 2046 | + s9 = ( |
| 2047 | + pd.Series(pd.period_range(start="2017-01-01", end="2017-02-01", freq="1D")) |
| 2048 | + .diff() |
| 2049 | + .iloc[1:] |
| 2050 | + ) |
| 2051 | + check(assert_type(s9.to_numpy(), np_1darray[np.object_]), np_1darray, Day) |
| 2052 | + |
| 2053 | + s10 = pd.Series(pd.date_range(start="2017-01-01", end="2017-02-01")) |
| 2054 | + check( |
| 2055 | + assert_type(s10.to_numpy(), np_1darray[np.datetime64]), |
| 2056 | + np_1darray, |
| 2057 | + np.datetime64, |
| 2058 | + ) |
| 2059 | + |
| 2060 | + s11 = pd.Series( |
| 2061 | + [datetime.datetime.now().date(), datetime.datetime.now().date()] |
| 2062 | + ).diff() |
| 2063 | + check( |
| 2064 | + assert_type(s11.to_numpy(), np_1darray[np.timedelta64]), |
| 2065 | + np_1darray, |
| 2066 | + np.timedelta64, |
| 2067 | + ) |
| 2068 | + |
| 2069 | + |
1993 | 2070 | def test_where() -> None: |
1994 | 2071 | s = pd.Series([1, 2, 3], dtype=int) |
1995 | 2072 |
|
|
0 commit comments