MCPcopy Create free account
hub / github.com/apache/arrow / test_convert_to_extension_array

Function test_convert_to_extension_array

python/pyarrow/tests/test_pandas.py:4456–4484  ·  view source on GitHub ↗
(monkeypatch)

Source from the content-addressed store, hash-verified

4454
4455
4456def test_convert_to_extension_array(monkeypatch):
4457 # table converted from dataframe with extension types (so pandas_metadata
4458 # has this information)
4459 df = pd.DataFrame(
4460 {'a': [1, 2, 3], 'b': pd.array([2, 3, 4], dtype='Int64'),
4461 'c': [4, 5, 6]})
4462 table = pa.table(df)
4463
4464 # Int64Dtype is recognized -> convert to extension block by default
4465 # for a proper roundtrip
4466 result = table.to_pandas()
4467 assert _get_mgr(result).blocks[0].values.dtype == np.dtype("int64")
4468 assert _get_mgr(result).blocks[1].values.dtype == pd.Int64Dtype()
4469 tm.assert_frame_equal(result, df)
4470
4471 # test with missing values
4472 df2 = pd.DataFrame({'a': pd.array([1, 2, None], dtype='Int64')})
4473 table2 = pa.table(df2)
4474 result = table2.to_pandas()
4475 assert _get_mgr(result).blocks[0].values.dtype == pd.Int64Dtype()
4476 tm.assert_frame_equal(result, df2)
4477
4478 # monkeypatch pandas Int64Dtype to *not* have the protocol method
4479 monkeypatch.delattr(
4480 pd.core.arrays.integer.NumericDtype, "__from_arrow__")
4481 # Int64Dtype has no __from_arrow__ -> use normal conversion
4482 result = table.to_pandas()
4483 assert len(_get_mgr(result).blocks) == 1
4484 assert _get_mgr(result).blocks[0].values.dtype == np.dtype("int64")
4485
4486
4487class MyCustomIntegerType(pa.ExtensionType):

Callers

nothing calls this directly

Calls 4

_get_mgrFunction · 0.85
lenFunction · 0.85
dtypeMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected