Python pandas.core.dtypes.cast.maybe_downcast_to_dtype() Examples
The following are 22
code examples of pandas.core.dtypes.cast.maybe_downcast_to_dtype().
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.core.dtypes.cast
, or try the search function
.
Example #1
Source File: groupby.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def _try_cast(self, result, obj, numeric_only=False): """ try to cast the result to our obj original type, we may have roundtripped thru object in the mean-time if numeric_only is True, then only try to cast numerics and not datetimelikes """ if obj.ndim > 1: dtype = obj.values.dtype else: dtype = obj.dtype if not is_scalar(result): if numeric_only and is_numeric_dtype(dtype) or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) return result
Example #2
Source File: groupby.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def _try_cast(self, result, obj, numeric_only=False): """ try to cast the result to our obj original type, we may have roundtripped thru object in the mean-time if numeric_only is True, then only try to cast numerics and not datetimelikes """ if obj.ndim > 1: dtype = obj.values.dtype else: dtype = obj.dtype if not is_scalar(result): if numeric_only and is_numeric_dtype(dtype) or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) return result
Example #3
Source File: groupby.py From vnpy_crypto with MIT License | 6 votes |
def _try_cast(self, result, obj, numeric_only=False): """ try to cast the result to our obj original type, we may have roundtripped thru object in the mean-time if numeric_only is True, then only try to cast numerics and not datetimelikes """ if obj.ndim > 1: dtype = obj.values.dtype else: dtype = obj.dtype if not is_scalar(result): if numeric_only and is_numeric_dtype(dtype) or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) return result
Example #4
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_downcast_booleans(): # see gh-16875: coercing of booleans. ser = Series([True, True, False]) result = maybe_downcast_to_dtype(ser, np.dtype(np.float64)) expected = ser tm.assert_series_equal(result, expected)
Example #5
Source File: groupby.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def transform(self, func, *args, **kwargs): func = self._is_cython_func(func) or func # if string function if isinstance(func, compat.string_types): if func in _cython_transforms: # cythonized transform return getattr(self, func)(*args, **kwargs) else: # cythonized aggregation and merge return self._transform_fast( lambda: getattr(self, func)(*args, **kwargs)) # reg transform klass = self._selected_obj.__class__ results = [] wrapper = lambda x: func(x, *args, **kwargs) for name, group in self: object.__setattr__(group, 'name', name) res = wrapper(group) if hasattr(res, 'values'): res = res.values indexer = self._get_index(name) s = klass(res, indexer) results.append(s) from pandas.core.reshape.concat import concat result = concat(results).sort_index() # we will only try to coerce the result type if # we have a numeric dtype, as these are *always* udfs # the cython take a different path (and casting) dtype = self._selected_obj.dtype if is_numeric_dtype(dtype): result = maybe_downcast_to_dtype(result, dtype) result.name = self._selected_obj.name result.index = self._selected_obj.index return result
Example #6
Source File: groupby.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def transform(self, func, *args, **kwargs): func = self._is_cython_func(func) or func # if string function if isinstance(func, compat.string_types): if func in _cython_transforms: # cythonized transform return getattr(self, func)(*args, **kwargs) else: # cythonized aggregation and merge return self._transform_fast( lambda: getattr(self, func)(*args, **kwargs)) # reg transform klass = self._selected_obj.__class__ results = [] wrapper = lambda x: func(x, *args, **kwargs) for name, group in self: object.__setattr__(group, 'name', name) res = wrapper(group) if hasattr(res, 'values'): res = res.values indexer = self._get_index(name) s = klass(res, indexer) results.append(s) from pandas.core.reshape.concat import concat result = concat(results).sort_index() # we will only try to coerce the result type if # we have a numeric dtype, as these are *always* udfs # the cython take a different path (and casting) dtype = self._selected_obj.dtype if is_numeric_dtype(dtype): result = maybe_downcast_to_dtype(result, dtype) result.name = self._selected_obj.name result.index = self._selected_obj.index return result
Example #7
Source File: groupby.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _try_cast(self, result, obj, numeric_only=False): """ Try to cast the result to our obj original type, we may have roundtripped through object in the mean-time. If numeric_only is True, then only try to cast numerics and not datetimelikes. """ if obj.ndim > 1: dtype = obj._values.dtype else: dtype = obj.dtype if not is_scalar(result): if is_extension_array_dtype(dtype): # The function can return something of any type, so check # if the type is compatible with the calling EA. try: result = obj._values._from_sequence(result, dtype=dtype) except Exception: # https://github.com/pandas-dev/pandas/issues/22850 # pandas has no control over what 3rd-party ExtensionArrays # do in _values_from_sequence. We still want ops to work # though, so we catch any regular Exception. pass elif numeric_only and is_numeric_dtype(dtype) or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) return result
Example #8
Source File: generic.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def transform(self, func, *args, **kwargs): func = self._is_cython_func(func) or func # if string function if isinstance(func, compat.string_types): if func in base.cython_transforms: # cythonized transform return getattr(self, func)(*args, **kwargs) else: # cythonized aggregation and merge return self._transform_fast( lambda: getattr(self, func)(*args, **kwargs), func) # reg transform klass = self._selected_obj.__class__ results = [] wrapper = lambda x: func(x, *args, **kwargs) for name, group in self: object.__setattr__(group, 'name', name) res = wrapper(group) if hasattr(res, 'values'): res = res.values indexer = self._get_index(name) s = klass(res, indexer) results.append(s) from pandas.core.reshape.concat import concat result = concat(results).sort_index() # we will only try to coerce the result type if # we have a numeric dtype, as these are *always* udfs # the cython take a different path (and casting) dtype = self._selected_obj.dtype if is_numeric_dtype(dtype): result = maybe_downcast_to_dtype(result, dtype) result.name = self._selected_obj.name result.index = self._selected_obj.index return result
Example #9
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_datetime_with_timezone(as_asi): # see gh-15426 ts = Timestamp("2016-01-01 12:00:00", tz="US/Pacific") exp = DatetimeIndex([ts, ts]) obj = exp.asi8 if as_asi else exp res = maybe_downcast_to_dtype(obj, exp.dtype) tm.assert_index_equal(res, exp)
Example #10
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_downcast_conversion_empty(any_real_dtype): dtype = any_real_dtype arr = np.array([], dtype=dtype) result = maybe_downcast_to_dtype(arr, "int64") tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64))
Example #11
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_downcast_conversion_nan(float_dtype): dtype = float_dtype data = [1.0, 2.0, np.nan] expected = np.array(data, dtype=dtype) arr = np.array(data, dtype=dtype) result = maybe_downcast_to_dtype(arr, "infer") tm.assert_almost_equal(result, expected)
Example #12
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_downcast_conversion_no_nan(any_real_dtype): dtype = any_real_dtype expected = np.array([1, 2]) arr = np.array([1.0, 2.0], dtype=dtype) result = maybe_downcast_to_dtype(arr, "infer") tm.assert_almost_equal(result, expected, check_dtype=False)
Example #13
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_downcast(arr, expected, dtype): result = maybe_downcast_to_dtype(arr, dtype) tm.assert_numpy_array_equal(result, expected)
Example #14
Source File: test_downcast.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_downcast(arr, expected, dtype): result = maybe_downcast_to_dtype(arr, dtype) tm.assert_numpy_array_equal(result, expected)
Example #15
Source File: groupby.py From vnpy_crypto with MIT License | 5 votes |
def transform(self, func, *args, **kwargs): func = self._is_cython_func(func) or func # if string function if isinstance(func, compat.string_types): if func in _cython_transforms: # cythonized transform return getattr(self, func)(*args, **kwargs) else: # cythonized aggregation and merge return self._transform_fast( lambda: getattr(self, func)(*args, **kwargs), func) # reg transform klass = self._selected_obj.__class__ results = [] wrapper = lambda x: func(x, *args, **kwargs) for name, group in self: object.__setattr__(group, 'name', name) res = wrapper(group) if hasattr(res, 'values'): res = res.values indexer = self._get_index(name) s = klass(res, indexer) results.append(s) from pandas.core.reshape.concat import concat result = concat(results).sort_index() # we will only try to coerce the result type if # we have a numeric dtype, as these are *always* udfs # the cython take a different path (and casting) dtype = self._selected_obj.dtype if is_numeric_dtype(dtype): result = maybe_downcast_to_dtype(result, dtype) result.name = self._selected_obj.name result.index = self._selected_obj.index return result
Example #16
Source File: groupby.py From recruit with Apache License 2.0 | 5 votes |
def _try_cast(self, result, obj, numeric_only=False): """ Try to cast the result to our obj original type, we may have roundtripped through object in the mean-time. If numeric_only is True, then only try to cast numerics and not datetimelikes. """ if obj.ndim > 1: dtype = obj._values.dtype else: dtype = obj.dtype if not is_scalar(result): if is_extension_array_dtype(dtype): # The function can return something of any type, so check # if the type is compatible with the calling EA. try: result = obj._values._from_sequence(result, dtype=dtype) except Exception: # https://github.com/pandas-dev/pandas/issues/22850 # pandas has no control over what 3rd-party ExtensionArrays # do in _values_from_sequence. We still want ops to work # though, so we catch any regular Exception. pass elif numeric_only and is_numeric_dtype(dtype) or not numeric_only: result = maybe_downcast_to_dtype(result, dtype) return result
Example #17
Source File: generic.py From recruit with Apache License 2.0 | 5 votes |
def transform(self, func, *args, **kwargs): func = self._is_cython_func(func) or func # if string function if isinstance(func, compat.string_types): if func in base.cython_transforms: # cythonized transform return getattr(self, func)(*args, **kwargs) else: # cythonized aggregation and merge return self._transform_fast( lambda: getattr(self, func)(*args, **kwargs), func) # reg transform klass = self._selected_obj.__class__ results = [] wrapper = lambda x: func(x, *args, **kwargs) for name, group in self: object.__setattr__(group, 'name', name) res = wrapper(group) if hasattr(res, 'values'): res = res.values indexer = self._get_index(name) s = klass(res, indexer) results.append(s) from pandas.core.reshape.concat import concat result = concat(results).sort_index() # we will only try to coerce the result type if # we have a numeric dtype, as these are *always* udfs # the cython take a different path (and casting) dtype = self._selected_obj.dtype if is_numeric_dtype(dtype): result = maybe_downcast_to_dtype(result, dtype) result.name = self._selected_obj.name result.index = self._selected_obj.index return result
Example #18
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_datetime_with_timezone(as_asi): # see gh-15426 ts = Timestamp("2016-01-01 12:00:00", tz="US/Pacific") exp = DatetimeIndex([ts, ts]) obj = exp.asi8 if as_asi else exp res = maybe_downcast_to_dtype(obj, exp.dtype) tm.assert_index_equal(res, exp)
Example #19
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_downcast_conversion_empty(any_real_dtype): dtype = any_real_dtype arr = np.array([], dtype=dtype) result = maybe_downcast_to_dtype(arr, "int64") tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64))
Example #20
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_downcast_conversion_nan(float_dtype): dtype = float_dtype data = [1.0, 2.0, np.nan] expected = np.array(data, dtype=dtype) arr = np.array(data, dtype=dtype) result = maybe_downcast_to_dtype(arr, "infer") tm.assert_almost_equal(result, expected)
Example #21
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_downcast_conversion_no_nan(any_real_dtype): dtype = any_real_dtype expected = np.array([1, 2]) arr = np.array([1.0, 2.0], dtype=dtype) result = maybe_downcast_to_dtype(arr, "infer") tm.assert_almost_equal(result, expected, check_dtype=False)
Example #22
Source File: test_downcast.py From recruit with Apache License 2.0 | 5 votes |
def test_downcast_booleans(): # see gh-16875: coercing of booleans. ser = Series([True, True, False]) result = maybe_downcast_to_dtype(ser, np.dtype(np.float64)) expected = ser tm.assert_series_equal(result, expected)