Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl committed Feb 11, 2024
1 parent 093f4ad commit 6bea80b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 41 deletions.
18 changes: 6 additions & 12 deletions asv_bench/benchmarks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@


class ToNumeric:
params = ["ignore", "coerce"]
param_names = ["errors"]

def setup(self, errors):
def setup(self):
N = 10000
self.float = Series(np.random.randn(N))
self.numstr = self.float.astype("str")
self.str = Series(Index([f"i-{i}" for i in range(N)], dtype=object))

def time_from_float(self, errors):
to_numeric(self.float, errors=errors)
def time_from_float(self):
to_numeric(self.float, errors="coerce")

def time_from_numeric_str(self, errors):
to_numeric(self.numstr, errors=errors)
Expand Down Expand Up @@ -301,16 +298,13 @@ def time_convert_string_seconds(self):


class ToTimedeltaErrors:
params = ["coerce", "ignore"]
param_names = ["errors"]

def setup(self, errors):
def setup(self):
ints = np.random.randint(0, 60, size=10000)
self.arr = [f"{i} days" for i in ints]
self.arr[-1] = "apple"

def time_convert(self, errors):
to_timedelta(self.arr, errors=errors)
def time_convert(self):
to_timedelta(self.arr, errors="coerce")


from .pandas_vb_common import setup # noqa: F401 isort:skip
34 changes: 5 additions & 29 deletions pandas/tests/tslibs/test_array_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,6 @@ def test_parsing_different_timezone_offsets():
assert result_tz is None


@pytest.mark.parametrize(
"data", [["-352.737091", "183.575577"], ["1", "2", "3", "4", "5"]]
)
def test_number_looking_strings_not_into_datetime(data):
# see gh-4601
#
# These strings don't look like datetimes, so
# they shouldn't be attempted to be converted.
arr = np.array(data, dtype=object)
result, _ = tslib.array_to_datetime(arr, errors="ignore")

tm.assert_numpy_array_equal(result, arr)


@pytest.mark.parametrize(
"invalid_date",
[
Expand Down Expand Up @@ -266,22 +252,12 @@ def test_coerce_outside_ns_bounds_one_valid():
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("errors", ["ignore", "coerce"])
def test_coerce_of_invalid_datetimes(errors):
def test_coerce_of_invalid_datetimes():
arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object)
kwargs = {"values": arr, "errors": errors}

if errors == "ignore":
# Without coercing, the presence of any invalid
# dates prevents any values from being converted.
result, _ = tslib.array_to_datetime(**kwargs)
tm.assert_numpy_array_equal(result, arr)
else: # coerce.
# With coercing, the invalid dates becomes iNaT
result, _ = tslib.array_to_datetime(arr, errors="coerce")
expected = ["2013-01-01T00:00:00.000000000", iNaT, iNaT]

tm.assert_numpy_array_equal(result, np.array(expected, dtype="M8[ns]"))
# With coercing, the invalid dates becomes iNaT
result, _ = tslib.array_to_datetime(arr, errors="coerce")
expected = ["2013-01-01T00:00:00.000000000", iNaT, iNaT]
tm.assert_numpy_array_equal(result, np.array(expected, dtype="M8[ns]"))


def test_to_datetime_barely_out_of_bounds():
Expand Down

0 comments on commit 6bea80b

Please sign in to comment.