def test_from_pandas_array(dtype): # GH#24615 data = np.array([1, 2, 3], dtype=dtype) arr = PandasArray(data) cls = {"M8[ns]": DatetimeArray, "m8[ns]": TimedeltaArray}[dtype] result = cls(arr) expected = cls(data) tm.assert_extension_array_equal(result, expected) result = cls._from_sequence(arr) expected = cls._from_sequence(data) tm.assert_extension_array_equal(result, expected) func = {"M8[ns]": sequence_to_dt64ns, "m8[ns]": sequence_to_td64ns}[dtype] result = func(arr)[0] expected = func(data)[0] tm.assert_equal(result, expected) func = {"M8[ns]": pd.to_datetime, "m8[ns]": pd.to_timedelta}[dtype] result = func(arr).array expected = func(data).array tm.assert_equal(result, expected) # Let's check the Indexes while we're here idx_cls = {"M8[ns]": DatetimeIndex, "m8[ns]": TimedeltaIndex}[dtype] result = idx_cls(arr) expected = idx_cls(data) tm.assert_index_equal(result, expected)
def test_empty_pandas_array(self): arr = PandasArray(np.array([1, 2])) dtype = arr.dtype shape = (3, 9) result = PandasArray._empty(shape, dtype=dtype) assert isinstance(result, PandasArray) assert result.dtype == dtype assert result.shape == shape
def test_to_dict_of_blocks_item_cache(): # Calling to_dict_of_blocks should not poison item_cache df = DataFrame({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "d"]}) df["c"] = PandasArray(np.array([1, 2, None, 3], dtype=object)) mgr = df._mgr assert len(mgr.blocks) == 3 # i.e. not consolidated ser = df["b"] # populations item_cache["b"] df._to_dict_of_blocks() # Check that the to_dict_of_blocks didn't break link between ser and df ser.values[0] = "foo" assert df.loc[0, "b"] == "foo" assert df["b"] is ser
def box_expected(expected, box_cls, transpose=True): """ Helper function to wrap the expected output of a test in a given box_class. Parameters ---------- expected : np.ndarray, Index, Series box_cls : {Index, Series, DataFrame} Returns ------- subclass of box_cls """ if box_cls is pd.array: if isinstance(expected, RangeIndex): # pd.array would return an IntegerArray expected = PandasArray(np.asarray(expected._values)) else: expected = pd.array(expected) elif box_cls is Index: expected = Index._with_infer(expected) elif box_cls is Series: expected = Series(expected) elif box_cls is DataFrame: expected = Series(expected).to_frame() if transpose: # for vector operations, we need a DataFrame to be a single-row, # not a single-column, in order to operate against non-DataFrame # vectors of the same length. But convert to two rows to avoid # single-row special cases in datetime arithmetic expected = expected.T expected = pd.concat([expected] * 2, ignore_index=True) elif box_cls is PeriodArray: # the PeriodArray constructor is not as flexible as period_array expected = period_array(expected) elif box_cls is DatetimeArray: expected = DatetimeArray(expected) elif box_cls is TimedeltaArray: expected = TimedeltaArray(expected) elif box_cls is np.ndarray: expected = np.array(expected) elif box_cls is to_array: expected = to_array(expected) else: raise NotImplementedError(box_cls) return expected
def array_values(self): """The array that Series.array returns""" arr = self.array if isinstance(arr, np.ndarray): arr = PandasArray(arr) return arr
def test_numpy_array(arr): ser = pd.Series(arr) result = ser.array expected = PandasArray(arr) tm.assert_extension_array_equal(result, expected)
def test_array_inference_fails(data): result = pd.array(data) expected = PandasArray(np.array(data, dtype=object)) tm.assert_extension_array_equal(result, expected)
IntegerArray, IntervalArray, SparseArray, StringArray, TimedeltaArray, ) from pandas.core.arrays import PandasArray, integer_array, period_array from pandas.tests.extension.decimal import DecimalArray, DecimalDtype, to_decimal @pytest.mark.parametrize( "data, dtype, expected", [ # Basic NumPy defaults. ([1, 2], None, IntegerArray._from_sequence([1, 2])), ([1, 2], object, PandasArray(np.array([1, 2], dtype=object))), ( [1, 2], np.dtype("float32"), PandasArray(np.array([1.0, 2.0], dtype=np.dtype("float32"))), ), (np.array([1, 2], dtype="int64"), None, IntegerArray._from_sequence([1, 2])), ( np.array([1.0, 2.0], dtype="float64"), None, FloatingArray._from_sequence([1.0, 2.0]), ), # String alias passes through to NumPy ([1, 2], "float32", PandasArray(np.array([1, 2], dtype="float32"))), # Period alias (
import pytest import pytz from pandas.core.dtypes.dtypes import registry import pandas as pd from pandas.api.extensions import register_extension_dtype from pandas.core.arrays import PandasArray, integer_array, period_array from pandas.tests.extension.decimal import ( DecimalArray, DecimalDtype, to_decimal) import pandas.util.testing as tm @pytest.mark.parametrize("data, dtype, expected", [ # Basic NumPy defaults. ([1, 2], None, PandasArray(np.array([1, 2]))), ([1, 2], object, PandasArray(np.array([1, 2], dtype=object))), ([1, 2], np.dtype('float32'), PandasArray(np.array([1., 2.0], dtype=np.dtype('float32')))), (np.array([1, 2]), None, PandasArray(np.array([1, 2]))), # String alias passes through to NumPy ([1, 2], 'float32', PandasArray(np.array([1, 2], dtype='float32'))), # Period alias ([pd.Period('2000', 'D'), pd.Period('2001', 'D')], 'Period[D]', period_array(['2000', '2001'], freq='D')), # Period dtype ([pd.Period('2000', 'D')], pd.PeriodDtype('D'), period_array(['2000'], freq='D')),