|
1 | 1 | import numpy as np
|
2 | 2 | import pytest
|
3 | 3 |
|
| 4 | +from pandas._libs import iNaT |
| 5 | + |
4 | 6 | from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
5 | 7 |
|
6 | 8 | import pandas as pd
|
@@ -168,3 +170,87 @@ def test_2d(self, order):
|
168 | 170 | res = DatetimeArray._from_sequence(arr)
|
169 | 171 | expected = DatetimeArray._from_sequence(arr.ravel()).reshape(arr.shape)
|
170 | 172 | tm.assert_datetime_array_equal(res, expected)
|
| 173 | + |
| 174 | + |
| 175 | +# ---------------------------------------------------------------------------- |
| 176 | +# Arrow interaction |
| 177 | + |
| 178 | + |
| 179 | +EXTREME_VALUES = [0, 123456789, None, iNaT, 2**63 - 1, -(2**63) + 1] |
| 180 | +FINE_TO_COARSE_SAFE = [123_000_000_000, None, -123_000_000_000] |
| 181 | +COARSE_TO_FINE_SAFE = [123, None, -123] |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.parametrize( |
| 185 | + ("pa_unit", "pd_unit", "pa_tz", "pd_tz", "data"), |
| 186 | + [ |
| 187 | + ("s", "s", "UTC", "UTC", EXTREME_VALUES), |
| 188 | + ("ms", "ms", "UTC", "Europe/Berlin", EXTREME_VALUES), |
| 189 | + ("us", "us", "US/Eastern", "UTC", EXTREME_VALUES), |
| 190 | + ("ns", "ns", "US/Central", "Asia/Kolkata", EXTREME_VALUES), |
| 191 | + ("ns", "s", "UTC", "UTC", FINE_TO_COARSE_SAFE), |
| 192 | + ("us", "ms", "UTC", "Europe/Berlin", FINE_TO_COARSE_SAFE), |
| 193 | + ("ms", "us", "US/Eastern", "UTC", COARSE_TO_FINE_SAFE), |
| 194 | + ("s", "ns", "US/Central", "Asia/Kolkata", COARSE_TO_FINE_SAFE), |
| 195 | + ], |
| 196 | +) |
| 197 | +def test_from_arrowtest_from_arrow_with_different_units_and_timezones_with_( |
| 198 | + pa_unit, pd_unit, pa_tz, pd_tz, data |
| 199 | +): |
| 200 | + pa = pytest.importorskip("pyarrow") |
| 201 | + |
| 202 | + pa_type = pa.timestamp(pa_unit, tz=pa_tz) |
| 203 | + arr = pa.array(data, type=pa_type) |
| 204 | + dtype = DatetimeTZDtype(unit=pd_unit, tz=pd_tz) |
| 205 | + |
| 206 | + result = dtype.__from_arrow__(arr) |
| 207 | + expected = DatetimeArray( |
| 208 | + np.array(data, dtype=f"datetime64[{pa_unit}]").astype(f"datetime64[{pd_unit}]"), |
| 209 | + dtype=dtype, |
| 210 | + ) |
| 211 | + tm.assert_extension_array_equal(result, expected) |
| 212 | + |
| 213 | + result = dtype.__from_arrow__(pa.chunked_array([arr])) |
| 214 | + tm.assert_extension_array_equal(result, expected) |
| 215 | + |
| 216 | + |
| 217 | +@pytest.mark.parametrize( |
| 218 | + ("unit", "tz"), |
| 219 | + [ |
| 220 | + ("s", "UTC"), |
| 221 | + ("ms", "Europe/Berlin"), |
| 222 | + ("us", "US/Eastern"), |
| 223 | + ("ns", "Asia/Kolkata"), |
| 224 | + ("ns", "UTC"), |
| 225 | + ], |
| 226 | +) |
| 227 | +def test_from_arrow_from_empty(unit, tz): |
| 228 | + pa = pytest.importorskip("pyarrow") |
| 229 | + |
| 230 | + data = [] |
| 231 | + arr = pa.array(data) |
| 232 | + dtype = DatetimeTZDtype(unit=unit, tz=tz) |
| 233 | + |
| 234 | + result = dtype.__from_arrow__(arr) |
| 235 | + expected = DatetimeArray(np.array(data, dtype=f"datetime64[{unit}]")) |
| 236 | + expected = expected.tz_localize(tz=tz) |
| 237 | + tm.assert_extension_array_equal(result, expected) |
| 238 | + |
| 239 | + result = dtype.__from_arrow__(pa.chunked_array([arr])) |
| 240 | + tm.assert_extension_array_equal(result, expected) |
| 241 | + |
| 242 | + |
| 243 | +def test_from_arrow_from_integers(): |
| 244 | + pa = pytest.importorskip("pyarrow") |
| 245 | + |
| 246 | + data = [0, 123456789, None, 2**63 - 1, iNaT, -123456789] |
| 247 | + arr = pa.array(data) |
| 248 | + dtype = DatetimeTZDtype(unit="ns", tz="UTC") |
| 249 | + |
| 250 | + result = dtype.__from_arrow__(arr) |
| 251 | + expected = DatetimeArray(np.array(data, dtype="datetime64[ns]")) |
| 252 | + expected = expected.tz_localize("UTC") |
| 253 | + tm.assert_extension_array_equal(result, expected) |
| 254 | + |
| 255 | + result = dtype.__from_arrow__(pa.chunked_array([arr])) |
| 256 | + tm.assert_extension_array_equal(result, expected) |
0 commit comments