|
3 | 3 | in core.internals
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +import datetime |
| 7 | + |
| 8 | +import numpy as np |
6 | 9 | import pytest
|
7 | 10 |
|
8 | 11 | import pandas as pd
|
9 | 12 | import pandas._testing as tm
|
| 13 | +from pandas.api.internals import create_dataframe_from_blocks |
10 | 14 | from pandas.core import internals
|
11 | 15 | from pandas.core.internals import api
|
12 | 16 |
|
@@ -71,3 +75,91 @@ def test_create_block_manager_from_blocks_deprecated():
|
71 | 75 | )
|
72 | 76 | with tm.assert_produces_warning(DeprecationWarning, match=msg):
|
73 | 77 | internals.create_block_manager_from_blocks
|
| 78 | + |
| 79 | + |
| 80 | +def test_create_dataframe_from_blocks(float_frame): |
| 81 | + block = float_frame._mgr.blocks[0] |
| 82 | + index = float_frame.index.copy() |
| 83 | + columns = float_frame.columns.copy() |
| 84 | + |
| 85 | + result = create_dataframe_from_blocks( |
| 86 | + [(block.values, block.mgr_locs.as_array)], index=index, columns=columns |
| 87 | + ) |
| 88 | + tm.assert_frame_equal(result, float_frame) |
| 89 | + |
| 90 | + |
| 91 | +def test_create_dataframe_from_blocks_types(): |
| 92 | + df = pd.DataFrame( |
| 93 | + { |
| 94 | + "int": list(range(1, 4)), |
| 95 | + "uint": np.arange(3, 6).astype("uint8"), |
| 96 | + "float": [2.0, np.nan, 3.0], |
| 97 | + "bool": np.array([True, False, True]), |
| 98 | + "boolean": pd.array([True, False, None], dtype="boolean"), |
| 99 | + "string": list("abc"), |
| 100 | + "datetime": pd.date_range("20130101", periods=3), |
| 101 | + "datetimetz": pd.date_range("20130101", periods=3).tz_localize( |
| 102 | + "Europe/Brussels" |
| 103 | + ), |
| 104 | + "timedelta": pd.timedelta_range("1 day", periods=3), |
| 105 | + "period": pd.period_range("2012-01-01", periods=3, freq="D"), |
| 106 | + "categorical": pd.Categorical(["a", "b", "a"]), |
| 107 | + "interval": pd.IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)]), |
| 108 | + } |
| 109 | + ) |
| 110 | + |
| 111 | + result = create_dataframe_from_blocks( |
| 112 | + [(block.values, block.mgr_locs.as_array) for block in df._mgr.blocks], |
| 113 | + index=df.index, |
| 114 | + columns=df.columns, |
| 115 | + ) |
| 116 | + tm.assert_frame_equal(result, df) |
| 117 | + |
| 118 | + |
| 119 | +def test_create_dataframe_from_blocks_datetimelike(): |
| 120 | + # extension dtypes that have an exact matching numpy dtype can also be |
| 121 | + # be passed as a numpy array |
| 122 | + index, columns = pd.RangeIndex(3), pd.Index(["a", "b", "c", "d"]) |
| 123 | + |
| 124 | + block_array1 = np.arange( |
| 125 | + datetime.datetime(2020, 1, 1), |
| 126 | + datetime.datetime(2020, 1, 7), |
| 127 | + step=datetime.timedelta(1), |
| 128 | + ).reshape((2, 3)) |
| 129 | + block_array2 = np.arange( |
| 130 | + datetime.timedelta(1), datetime.timedelta(7), step=datetime.timedelta(1) |
| 131 | + ).reshape((2, 3)) |
| 132 | + result = create_dataframe_from_blocks( |
| 133 | + [(block_array1, np.array([0, 2])), (block_array2, np.array([1, 3]))], |
| 134 | + index=index, |
| 135 | + columns=columns, |
| 136 | + ) |
| 137 | + expected = pd.DataFrame( |
| 138 | + { |
| 139 | + "a": pd.date_range("2020-01-01", periods=3, unit="us"), |
| 140 | + "b": pd.timedelta_range("1 days", periods=3, unit="us"), |
| 141 | + "c": pd.date_range("2020-01-04", periods=3, unit="us"), |
| 142 | + "d": pd.timedelta_range("4 days", periods=3, unit="us"), |
| 143 | + } |
| 144 | + ) |
| 145 | + tm.assert_frame_equal(result, expected) |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( |
| 149 | + "array", |
| 150 | + [ |
| 151 | + pd.date_range("2020-01-01", periods=3), |
| 152 | + pd.date_range("2020-01-01", periods=3, tz="UTC"), |
| 153 | + pd.period_range("2012-01-01", periods=3, freq="D"), |
| 154 | + pd.timedelta_range("1 day", periods=3), |
| 155 | + ], |
| 156 | +) |
| 157 | +def test_create_dataframe_from_blocks_1dEA(array): |
| 158 | + # ExtensionArrays can be passed as 1D even if stored under the hood as 2D |
| 159 | + df = pd.DataFrame({"a": array}) |
| 160 | + |
| 161 | + block = df._mgr.blocks[0] |
| 162 | + result = create_dataframe_from_blocks( |
| 163 | + [(block.values[0], block.mgr_locs.as_array)], index=df.index, columns=df.columns |
| 164 | + ) |
| 165 | + tm.assert_frame_equal(result, df) |
0 commit comments