Python pandas._libs.lib.maybe_convert_numeric() Examples

The following are 30 code examples of pandas._libs.lib.maybe_convert_numeric(). 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._libs.lib , or try the search function .
Example #1
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan(self, coerce, arr):
        expected = arr.astype(float) if coerce else arr.copy()
        result = lib.maybe_convert_numeric(arr, set(),
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #2
Source File: test_inference.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_scientific_no_exponent(self):
        # See PR 12215
        arr = np.array(['42E', '2E', '99e', '6e'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        assert np.all(np.isnan(result)) 
Example #3
Source File: test_inference.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_convert_non_hashable(self):
        # GH13324
        # make sure that we are handing non-hashables
        arr = np.array([[10.0, 2], 1.0, 'apple'])
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan])) 
Example #4
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_maybe_convert_numeric_infinities(self):
        # see gh-13274
        infinities = ['inf', 'inF', 'iNf', 'Inf',
                      'iNF', 'InF', 'INf', 'INF']
        na_values = {'', 'NULL', 'nan'}

        pos = np.array(['inf'], dtype=np.float64)
        neg = np.array(['-inf'], dtype=np.float64)

        msg = "Unable to parse string"

        for infinity in infinities:
            for maybe_int in (True, False):
                out = lib.maybe_convert_numeric(
                    np.array([infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['-' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, neg)

                out = lib.maybe_convert_numeric(
                    np.array([u(infinity)], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['+' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                # too many characters
                with pytest.raises(ValueError, match=msg):
                    lib.maybe_convert_numeric(
                        np.array(['foo_' + infinity], dtype=object),
                        na_values, maybe_int) 
Example #5
Source File: test_inference.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan_values(self, coerce):
        arr = np.array([2**63, 2**63 + 1], dtype=object)
        na_values = {2**63}

        expected = (np.array([np.nan, 2**63 + 1], dtype=float)
                    if coerce else arr.copy())
        result = lib.maybe_convert_numeric(arr, na_values,
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #6
Source File: test_inference.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_int64_uint64(self, case, coerce):
        expected = case.astype(float) if coerce else case.copy()
        result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #7
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_maybe_convert_numeric_infinities(self):
        # see gh-13274
        infinities = ['inf', 'inF', 'iNf', 'Inf',
                      'iNF', 'InF', 'INf', 'INF']
        na_values = set(['', 'NULL', 'nan'])

        pos = np.array(['inf'], dtype=np.float64)
        neg = np.array(['-inf'], dtype=np.float64)

        msg = "Unable to parse string"

        for infinity in infinities:
            for maybe_int in (True, False):
                out = lib.maybe_convert_numeric(
                    np.array([infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['-' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, neg)

                out = lib.maybe_convert_numeric(
                    np.array([u(infinity)], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['+' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                # too many characters
                with tm.assert_raises_regex(ValueError, msg):
                    lib.maybe_convert_numeric(
                        np.array(['foo_' + infinity], dtype=object),
                        na_values, maybe_int) 
Example #8
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_maybe_convert_numeric_post_floatify_nan(self):
        # see gh-13314
        data = np.array(['1.200', '-999.000', '4.500'], dtype=object)
        expected = np.array([1.2, np.nan, 4.5], dtype=np.float64)
        nan_values = set([-999, -999.0])

        for coerce_type in (True, False):
            out = lib.maybe_convert_numeric(data, nan_values, coerce_type)
            tm.assert_numpy_array_equal(out, expected) 
Example #9
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_convert_infs(self):
        arr = np.array(['inf', 'inf', 'inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64

        arr = np.array(['-inf', '-inf', '-inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64 
Example #10
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_scientific_no_exponent(self):
        # See PR 12215
        arr = np.array(['42E', '2E', '99e', '6e'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        assert np.all(np.isnan(result)) 
Example #11
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_convert_non_hashable(self):
        # GH13324
        # make sure that we are handing non-hashables
        arr = np.array([[10.0, 2], 1.0, 'apple'])
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan])) 
Example #12
Source File: test_inference.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan(self, coerce, arr):
        expected = arr.astype(float) if coerce else arr.copy()
        result = lib.maybe_convert_numeric(arr, set(),
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #13
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan_values(self, coerce):
        arr = np.array([2**63, 2**63 + 1], dtype=object)
        na_values = set([2**63])

        expected = (np.array([np.nan, 2**63 + 1], dtype=float)
                    if coerce else arr.copy())
        result = lib.maybe_convert_numeric(arr, na_values,
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #14
Source File: test_inference.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_convert_numeric_int64_uint64(self, case, coerce):
        expected = case.astype(float) if coerce else case.copy()
        result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #15
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_maybe_convert_numeric_infinities(self):
        # see gh-13274
        infinities = ['inf', 'inF', 'iNf', 'Inf',
                      'iNF', 'InF', 'INf', 'INF']
        na_values = set(['', 'NULL', 'nan'])

        pos = np.array(['inf'], dtype=np.float64)
        neg = np.array(['-inf'], dtype=np.float64)

        msg = "Unable to parse string"

        for infinity in infinities:
            for maybe_int in (True, False):
                out = lib.maybe_convert_numeric(
                    np.array([infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['-' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, neg)

                out = lib.maybe_convert_numeric(
                    np.array([u(infinity)], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                out = lib.maybe_convert_numeric(
                    np.array(['+' + infinity], dtype=object),
                    na_values, maybe_int)
                tm.assert_numpy_array_equal(out, pos)

                # too many characters
                with tm.assert_raises_regex(ValueError, msg):
                    lib.maybe_convert_numeric(
                        np.array(['foo_' + infinity], dtype=object),
                        na_values, maybe_int) 
Example #16
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_maybe_convert_numeric_post_floatify_nan(self, coerce):
        # see gh-13314
        data = np.array(['1.200', '-999.000', '4.500'], dtype=object)
        expected = np.array([1.2, np.nan, 4.5], dtype=np.float64)
        nan_values = set([-999, -999.0])

        out = lib.maybe_convert_numeric(data, nan_values, coerce)
        tm.assert_numpy_array_equal(out, expected) 
Example #17
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_convert_infs(self):
        arr = np.array(['inf', 'inf', 'inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64

        arr = np.array(['-inf', '-inf', '-inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64 
Example #18
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_scientific_no_exponent(self):
        # See PR 12215
        arr = np.array(['42E', '2E', '99e', '6e'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        assert np.all(np.isnan(result)) 
Example #19
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_convert_non_hashable(self):
        # GH13324
        # make sure that we are handing non-hashables
        arr = np.array([[10.0, 2], 1.0, 'apple'])
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan])) 
Example #20
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_convert_numeric_uint64_nan(self, coerce, arr):
        expected = arr.astype(float) if coerce else arr.copy()
        result = lib.maybe_convert_numeric(arr, set(),
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #21
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_convert_numeric_uint64_nan_values(self, coerce):
        arr = np.array([2**63, 2**63 + 1], dtype=object)
        na_values = set([2**63])

        expected = (np.array([np.nan, 2**63 + 1], dtype=float)
                    if coerce else arr.copy())
        result = lib.maybe_convert_numeric(arr, na_values,
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #22
Source File: test_inference.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_convert_numeric_int64_uint64(self, case, coerce):
        expected = case.astype(float) if coerce else case.copy()
        result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #23
Source File: test_inference.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_scientific_no_exponent(self):
        # See PR 12215
        arr = np.array(['42E', '2E', '99e', '6e'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        assert np.all(np.isnan(result)) 
Example #24
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_maybe_convert_numeric_post_floatify_nan(self, coerce):
        # see gh-13314
        data = np.array(['1.200', '-999.000', '4.500'], dtype=object)
        expected = np.array([1.2, np.nan, 4.5], dtype=np.float64)
        nan_values = {-999, -999.0}

        out = lib.maybe_convert_numeric(data, nan_values, coerce)
        tm.assert_numpy_array_equal(out, expected) 
Example #25
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_infs(self):
        arr = np.array(['inf', 'inf', 'inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64

        arr = np.array(['-inf', '-inf', '-inf'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False)
        assert result.dtype == np.float64 
Example #26
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_scientific_no_exponent(self):
        # See PR 12215
        arr = np.array(['42E', '2E', '99e', '6e'], dtype='O')
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        assert np.all(np.isnan(result)) 
Example #27
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_non_hashable(self):
        # GH13324
        # make sure that we are handing non-hashables
        arr = np.array([[10.0, 2], 1.0, 'apple'])
        result = lib.maybe_convert_numeric(arr, set(), False, True)
        tm.assert_numpy_array_equal(result, np.array([np.nan, 1.0, np.nan])) 
Example #28
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan(self, coerce, arr):
        expected = arr.astype(float) if coerce else arr.copy()
        result = lib.maybe_convert_numeric(arr, set(),
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #29
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_uint64_nan_values(self, coerce):
        arr = np.array([2**63, 2**63 + 1], dtype=object)
        na_values = {2**63}

        expected = (np.array([np.nan, 2**63 + 1], dtype=float)
                    if coerce else arr.copy())
        result = lib.maybe_convert_numeric(arr, na_values,
                                           coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected) 
Example #30
Source File: test_inference.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_convert_numeric_int64_uint64(self, case, coerce):
        expected = case.astype(float) if coerce else case.copy()
        result = lib.maybe_convert_numeric(case, set(), coerce_numeric=coerce)
        tm.assert_almost_equal(result, expected)