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