|
21 | 21 | import pytest
|
22 | 22 |
|
23 | 23 | from pandas.compat import (
|
| 24 | + is_ci_environment, |
| 25 | + is_platform_windows, |
24 | 26 | pa_version_under2p0,
|
25 | 27 | pa_version_under3p0,
|
26 | 28 | pa_version_under4p0,
|
27 | 29 | pa_version_under6p0,
|
| 30 | + pa_version_under7p0, |
28 | 31 | pa_version_under8p0,
|
29 | 32 | pa_version_under9p0,
|
30 | 33 | )
|
|
35 | 38 |
|
36 | 39 | pa = pytest.importorskip("pyarrow", minversion="1.0.1")
|
37 | 40 |
|
| 41 | +from pandas.core.arrays.arrow.array import ArrowExtensionArray |
| 42 | + |
38 | 43 | from pandas.core.arrays.arrow.dtype import ArrowDtype # isort:skip
|
39 | 44 |
|
40 | 45 |
|
@@ -222,6 +227,96 @@ def test_from_dtype(self, data, request):
|
222 | 227 | )
|
223 | 228 | super().test_from_dtype(data)
|
224 | 229 |
|
| 230 | + def test_from_sequence_pa_array(self, data, request): |
| 231 | + # https://github.com/pandas-dev/pandas/pull/47034#discussion_r955500784 |
| 232 | + # data._data = pa.ChunkedArray |
| 233 | + if pa_version_under3p0: |
| 234 | + request.node.add_marker( |
| 235 | + pytest.mark.xfail( |
| 236 | + reason="ChunkedArray has no attribute combine_chunks", |
| 237 | + ) |
| 238 | + ) |
| 239 | + result = type(data)._from_sequence(data._data) |
| 240 | + tm.assert_extension_array_equal(result, data) |
| 241 | + assert isinstance(result._data, pa.ChunkedArray) |
| 242 | + |
| 243 | + result = type(data)._from_sequence(data._data.combine_chunks()) |
| 244 | + tm.assert_extension_array_equal(result, data) |
| 245 | + assert isinstance(result._data, pa.ChunkedArray) |
| 246 | + |
| 247 | + def test_from_sequence_pa_array_notimplemented(self, request): |
| 248 | + if pa_version_under6p0: |
| 249 | + request.node.add_marker( |
| 250 | + pytest.mark.xfail( |
| 251 | + raises=AttributeError, |
| 252 | + reason="month_day_nano_interval not implemented by pyarrow.", |
| 253 | + ) |
| 254 | + ) |
| 255 | + with pytest.raises(NotImplementedError, match="Converting strings to"): |
| 256 | + ArrowExtensionArray._from_sequence_of_strings( |
| 257 | + ["12-1"], dtype=pa.month_day_nano_interval() |
| 258 | + ) |
| 259 | + |
| 260 | + def test_from_sequence_of_strings_pa_array(self, data, request): |
| 261 | + pa_dtype = data.dtype.pyarrow_dtype |
| 262 | + if pa_version_under3p0: |
| 263 | + request.node.add_marker( |
| 264 | + pytest.mark.xfail( |
| 265 | + reason="ChunkedArray has no attribute combine_chunks", |
| 266 | + ) |
| 267 | + ) |
| 268 | + elif pa.types.is_time64(pa_dtype) and pa_dtype.equals("time64[ns]"): |
| 269 | + request.node.add_marker( |
| 270 | + pytest.mark.xfail( |
| 271 | + reason="Nanosecond time parsing not supported.", |
| 272 | + ) |
| 273 | + ) |
| 274 | + elif pa.types.is_duration(pa_dtype): |
| 275 | + request.node.add_marker( |
| 276 | + pytest.mark.xfail( |
| 277 | + raises=pa.ArrowNotImplementedError, |
| 278 | + reason=f"pyarrow doesn't support parsing {pa_dtype}", |
| 279 | + ) |
| 280 | + ) |
| 281 | + elif pa.types.is_boolean(pa_dtype): |
| 282 | + request.node.add_marker( |
| 283 | + pytest.mark.xfail( |
| 284 | + reason="Iterating over ChunkedArray[bool] returns PyArrow scalars.", |
| 285 | + ) |
| 286 | + ) |
| 287 | + elif pa.types.is_timestamp(pa_dtype) and pa_dtype.tz is not None: |
| 288 | + if pa_version_under7p0: |
| 289 | + request.node.add_marker( |
| 290 | + pytest.mark.xfail( |
| 291 | + raises=pa.ArrowNotImplementedError, |
| 292 | + reason=f"pyarrow doesn't support string cast from {pa_dtype}", |
| 293 | + ) |
| 294 | + ) |
| 295 | + elif is_platform_windows() and is_ci_environment(): |
| 296 | + request.node.add_marker( |
| 297 | + pytest.mark.xfail( |
| 298 | + raises=pa.ArrowInvalid, |
| 299 | + reason=( |
| 300 | + "TODO: Set ARROW_TIMEZONE_DATABASE environment variable " |
| 301 | + "on CI to path to the tzdata for pyarrow." |
| 302 | + ), |
| 303 | + ) |
| 304 | + ) |
| 305 | + elif pa_version_under6p0 and pa.types.is_temporal(pa_dtype): |
| 306 | + request.node.add_marker( |
| 307 | + pytest.mark.xfail( |
| 308 | + raises=pa.ArrowNotImplementedError, |
| 309 | + reason=f"pyarrow doesn't support string cast from {pa_dtype}", |
| 310 | + ) |
| 311 | + ) |
| 312 | + pa_array = data._data.cast(pa.string()) |
| 313 | + result = type(data)._from_sequence_of_strings(pa_array, dtype=data.dtype) |
| 314 | + tm.assert_extension_array_equal(result, data) |
| 315 | + |
| 316 | + pa_array = pa_array.combine_chunks() |
| 317 | + result = type(data)._from_sequence_of_strings(pa_array, dtype=data.dtype) |
| 318 | + tm.assert_extension_array_equal(result, data) |
| 319 | + |
225 | 320 |
|
226 | 321 | @pytest.mark.xfail(
|
227 | 322 | raises=NotImplementedError, reason="pyarrow.ChunkedArray backing is 1D."
|
|
0 commit comments