Python pandas.array() Examples
The following are 30
code examples of pandas.array().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas
, or try the search function
.
Example #1
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_array_interface_tz(self): tz = "US/Central" data = DatetimeArray(pd.date_range('2017', periods=2, tz=tz)) result = np.asarray(data) expected = np.array([pd.Timestamp('2017-01-01T00:00:00', tz=tz), pd.Timestamp('2017-01-02T00:00:00', tz=tz)], dtype=object) tm.assert_numpy_array_equal(result, expected) result = np.asarray(data, dtype=object) tm.assert_numpy_array_equal(result, expected) result = np.asarray(data, dtype='M8[ns]') expected = np.array(['2017-01-01T06:00:00', '2017-01-02T06:00:00'], dtype="M8[ns]") tm.assert_numpy_array_equal(result, expected)
Example #2
Source File: test_datetimes.py From recruit with Apache License 2.0 | 6 votes |
def test_array_interface_tz(self): tz = "US/Central" data = DatetimeArray(pd.date_range('2017', periods=2, tz=tz)) result = np.asarray(data) expected = np.array([pd.Timestamp('2017-01-01T00:00:00', tz=tz), pd.Timestamp('2017-01-02T00:00:00', tz=tz)], dtype=object) tm.assert_numpy_array_equal(result, expected) result = np.asarray(data, dtype=object) tm.assert_numpy_array_equal(result, expected) result = np.asarray(data, dtype='M8[ns]') expected = np.array(['2017-01-01T06:00:00', '2017-01-02T06:00:00'], dtype="M8[ns]") tm.assert_numpy_array_equal(result, expected)
Example #3
Source File: base.py From fletcher with MIT License | 6 votes |
def kind(self) -> str: """Return a character code (one of 'biufcmMOSUV'), default 'O'. This should match the NumPy dtype used when the array is converted to an ndarray, which is probably 'O' for object if the extension type cannot be represented as a built-in NumPy type. See Also -------- numpy.dtype.kind """ if pa.types.is_date(self.arrow_dtype): return "O" else: return np.dtype(self.arrow_dtype.to_pandas_dtype()).kind
Example #4
Source File: test_utils.py From altair with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_sanitize_boolean_dtype(): df = pd.DataFrame( { "bool_none": pd.array([True, False, None], dtype="boolean"), "none": pd.array([None, None, None], dtype="boolean"), "bool": pd.array([True, False, True], dtype="boolean"), } ) df_clean = sanitize_dataframe(df) assert {col.dtype.name for _, col in df_clean.iteritems()} == {"object"} result_python = {col_name: list(col) for col_name, col in df_clean.iteritems()} assert result_python == { "bool_none": [True, False, None], "none": [None, None, None], "bool": [True, False, True], }
Example #5
Source File: test_utils.py From altair with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_sanitize_string_dtype(): df = pd.DataFrame( { "string_object": ["a", "b", "c", "d"], "string_string": pd.array(["a", "b", "c", "d"], dtype="string"), "string_object_null": ["a", "b", None, "d"], "string_string_null": pd.array(["a", "b", None, "d"], dtype="string"), } ) df_clean = sanitize_dataframe(df) assert {col.dtype.name for _, col in df_clean.iteritems()} == {"object"} result_python = {col_name: list(col) for col_name, col in df_clean.iteritems()} assert result_python == { "string_object": ["a", "b", "c", "d"], "string_string": ["a", "b", "c", "d"], "string_object_null": ["a", "b", None, "d"], "string_string_null": ["a", "b", None, "d"], }
Example #6
Source File: epacems.py From pudl with MIT License | 6 votes |
def add_facility_id_unit_id_epa(df): """ Harmonize columns that are added later. The datapackage validation checks for consistent column names, and these two columns aren't present before August 2008, so this adds them in. Args: df (pandas.DataFrame): A CEMS dataframe Returns: pandas.Dataframe: The same DataFrame guaranteed to have int facility_id and unit_id_epa cols. """ if ("facility_id" not in df.columns) or ("unit_id_epa" not in df.columns): # Can't just assign np.NaN and get an integer NaN, so make a new array # with the right shape: na_col = pd.array(np.full(df.shape[0], np.NaN), dtype="Int64") if "facility_id" not in df.columns: df["facility_id"] = na_col if "unit_id_epa" not in df.columns: df["unit_id_epa"] = na_col return df
Example #7
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_other_type_raises(self): with pytest.raises(ValueError, match="The dtype of 'values' is incorrect.*bool"): DatetimeArray(np.array([1, 2, 3], dtype='bool'))
Example #8
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_mixing_naive_tzaware_raises(self, meth): # GH#24569 arr = np.array([pd.Timestamp('2000'), pd.Timestamp('2000', tz='CET')]) msg = ('Cannot mix tz-aware with tz-naive values|' 'Tz-aware datetime.datetime cannot be converted ' 'to datetime64 unless utc=True') for obj in [arr, arr[::-1]]: # check that we raise regardless of whether naive is found # before aware or vice-versa with pytest.raises(ValueError, match=msg): meth(obj)
Example #9
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_incorrect_dtype_raises(self): with pytest.raises(ValueError, match="Unexpected value for 'dtype'."): DatetimeArray(np.array([1, 2, 3], dtype='i8'), dtype='category')
Example #10
Source File: base.py From fletcher with MIT License | 5 votes |
def example(self): """Get a simple array with example content.""" return self.construct_array_type()(_get_example(self.arrow_dtype))
Example #11
Source File: test_pandas_extension.py From fletcher with MIT License | 5 votes |
def data_for_sorting(fletcher_type, fletcher_array): """Length-3 array with a known sort order. This should be three items [B, C, A] with A < B < C """ return fletcher_array(fletcher_type.data_for_sorting, dtype=fletcher_type.dtype)
Example #12
Source File: test_pandas_extension.py From fletcher with MIT License | 5 votes |
def data_missing_for_sorting(fletcher_type, fletcher_array): """Length-3 array with a known sort order. This should be three items [B, NA, A] with A < B and NA missing. """ return fletcher_array( fletcher_type.data_missing_for_sorting, dtype=fletcher_type.dtype )
Example #13
Source File: base.py From fletcher with MIT License | 5 votes |
def _get_example(arrow_dtype: pa.DataType) -> pa.Array: if isinstance(arrow_dtype, pa.ListType): return pa.array( [None, _get_example(arrow_dtype.value_type).to_pylist()], type=arrow_dtype ) return _examples[arrow_dtype]
Example #14
Source File: base.py From fletcher with MIT License | 5 votes |
def type(self): """Return the scalar type for the array, e.g. ``int``. It's expected ``ExtensionArray[item]`` returns an instance of ``ExtensionDtype.type`` for scalar ``item``. """ return _python_type_map[self.arrow_dtype.id]
Example #15
Source File: base.py From fletcher with MIT License | 5 votes |
def construct_array_type(cls, *args) -> "Type[FletcherChunkedArray]": """ Return the array type associated with this dtype. Returns ------- type """ if len(args) > 0: raise NotImplementedError("construct_array_type does not support arguments") return FletcherChunkedArray
Example #16
Source File: base.py From fletcher with MIT License | 5 votes |
def size(self) -> int: """ Return the number of elements in this array. Returns ------- size : int """ return len(self.data)
Example #17
Source File: base.py From fletcher with MIT License | 5 votes |
def dtype(self) -> ExtensionDtype: """Return the ExtensionDtype of this array.""" return self._dtype
Example #18
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_mismatched_timezone_raises(self): arr = DatetimeArray(np.array(['2000-01-01T06:00:00'], dtype='M8[ns]'), dtype=DatetimeTZDtype(tz='US/Central')) dtype = DatetimeTZDtype(tz='US/Eastern') with pytest.raises(TypeError, match='Timezone of the array'): DatetimeArray(arr, dtype=dtype)
Example #19
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_from_pandas_array(self): arr = pd.array(np.arange(5, dtype=np.int64)) * 3600 * 10**9 result = DatetimeArray._from_sequence(arr, freq='infer') expected = pd.date_range('1970-01-01', periods=5, freq='H')._data tm.assert_datetime_array_equal(result, expected)
Example #20
Source File: test_datetimes.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_setitem_different_tz_raises(self): data = np.array([1, 2, 3], dtype='M8[ns]') arr = DatetimeArray(data, copy=False, dtype=DatetimeTZDtype(tz="US/Central")) with pytest.raises(ValueError, match="None"): arr[0] = pd.Timestamp('2000') with pytest.raises(ValueError, match="US/Central"): arr[0] = pd.Timestamp('2000', tz="US/Eastern")
Example #21
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_array_unboxes(box): data = box([decimal.Decimal('1'), decimal.Decimal('2')]) # make sure it works with pytest.raises(TypeError): DecimalArray2._from_sequence(data) result = pd.array(data, dtype='decimal2') expected = DecimalArray2._from_sequence(data.values) tm.assert_equal(result, expected)
Example #22
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_scalar_raises(): with pytest.raises(ValueError, match="Cannot pass scalar '1'"): pd.array(1) # --------------------------------------------------------------------------- # A couple dummy classes to ensure that Series and Indexes are unboxed before # getting to the EA classes.
Example #23
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_nd_raises(data): with pytest.raises(ValueError, match='PandasArray must be 1-dimensional'): pd.array(data)
Example #24
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_array_inference_fails(data): result = pd.array(data) expected = PandasArray(np.array(data, dtype=object)) tm.assert_extension_array_equal(result, expected)
Example #25
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_array_inference(data, expected): result = pd.array(data) tm.assert_equal(result, expected)
Example #26
Source File: test_array.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_array(data, dtype, expected): result = pd.array(data, dtype=dtype) tm.assert_equal(result, expected)
Example #27
Source File: test_numpy_nested.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_fillna_copy_series(self, data_missing): # The "scalar" for this array isn't a scalar. pass
Example #28
Source File: test_numpy_nested.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_fillna_copy_frame(self, data_missing): # The "scalar" for this array isn't a scalar. pass
Example #29
Source File: test_numpy_nested.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_shift_fill_value(self, data): # np.array shape inference. Shift implementation fails. super().test_shift_fill_value(data)
Example #30
Source File: test_numpy_nested.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_array_interface(self, data): # NumPy array shape inference pass