|
12 | 12 | from pandas.util._validators import validate_bool_kwarg
|
13 | 13 | from pandas.compat import range, map, zip
|
14 | 14 |
|
15 |
| -from pandas.core.dtypes.dtypes import ( |
16 |
| - ExtensionDtype, |
17 |
| - PandasExtensionDtype) |
18 | 15 | from pandas.core.dtypes.common import (
|
19 | 16 | _NS_DTYPE,
|
20 | 17 | is_datetimelike_v_numeric,
|
@@ -791,6 +788,11 @@ def _interleave(self):
|
791 | 788 | """
|
792 | 789 | dtype = _interleaved_dtype(self.blocks)
|
793 | 790 |
|
| 791 | + if is_extension_array_dtype(dtype): |
| 792 | + # TODO: https://github.com/pandas-dev/pandas/issues/22791 |
| 793 | + # Give EAs some input on what happens here. Sparse needs this. |
| 794 | + dtype = 'object' |
| 795 | + |
794 | 796 | result = np.empty(self.shape, dtype=dtype)
|
795 | 797 |
|
796 | 798 | if result.shape[0] == 0:
|
@@ -906,14 +908,25 @@ def fast_xs(self, loc):
|
906 | 908 |
|
907 | 909 | # unique
|
908 | 910 | dtype = _interleaved_dtype(self.blocks)
|
| 911 | + |
909 | 912 | n = len(items)
|
910 |
| - result = np.empty(n, dtype=dtype) |
| 913 | + if is_extension_array_dtype(dtype): |
| 914 | + # we'll eventually construct an ExtensionArray. |
| 915 | + result = np.empty(n, dtype=object) |
| 916 | + else: |
| 917 | + result = np.empty(n, dtype=dtype) |
| 918 | + |
911 | 919 | for blk in self.blocks:
|
912 | 920 | # Such assignment may incorrectly coerce NaT to None
|
913 | 921 | # result[blk.mgr_locs] = blk._slice((slice(None), loc))
|
914 | 922 | for i, rl in enumerate(blk.mgr_locs):
|
915 | 923 | result[rl] = blk._try_coerce_result(blk.iget((i, loc)))
|
916 | 924 |
|
| 925 | + if is_extension_array_dtype(dtype): |
| 926 | + result = dtype.construct_array_type()._from_sequence( |
| 927 | + result, dtype=dtype |
| 928 | + ) |
| 929 | + |
917 | 930 | return result
|
918 | 931 |
|
919 | 932 | def consolidate(self):
|
@@ -1855,16 +1868,22 @@ def _shape_compat(x):
|
1855 | 1868 |
|
1856 | 1869 |
|
1857 | 1870 | def _interleaved_dtype(blocks):
|
1858 |
| - if not len(blocks): |
1859 |
| - return None |
| 1871 | + # type: (List[Block]) -> Optional[Union[np.dtype, ExtensionDtype]] |
| 1872 | + """Find the common dtype for `blocks`. |
1860 | 1873 |
|
1861 |
| - dtype = find_common_type([b.dtype for b in blocks]) |
| 1874 | + Parameters |
| 1875 | + ---------- |
| 1876 | + blocks : List[Block] |
1862 | 1877 |
|
1863 |
| - # only numpy compat |
1864 |
| - if isinstance(dtype, (PandasExtensionDtype, ExtensionDtype)): |
1865 |
| - dtype = np.object |
| 1878 | + Returns |
| 1879 | + ------- |
| 1880 | + dtype : Optional[Union[np.dtype, ExtensionDtype]] |
| 1881 | + None is returned when `blocks` is empty. |
| 1882 | + """ |
| 1883 | + if not len(blocks): |
| 1884 | + return None |
1866 | 1885 |
|
1867 |
| - return dtype |
| 1886 | + return find_common_type([b.dtype for b in blocks]) |
1868 | 1887 |
|
1869 | 1888 |
|
1870 | 1889 | def _consolidate(blocks):
|
|
0 commit comments