Skip to content

Commit

Permalink
revert bits
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 15, 2023
1 parent 1be1709 commit 29854b7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 76 deletions.
2 changes: 0 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ cpdef array_to_datetime(
np.ndarray
May be datetime64[ns] or object dtype
tzinfo or None
str
Inferred resolution
"""
cdef:
Py_ssize_t i, n = values.size
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ from pandas._libs.tslibs.nattype cimport (
)
from pandas._libs.tslibs.np_datetime cimport (
NPY_DATETIMEUNIT,
NPY_FR_ns,
get_datetime64_unit,
import_pandas_datetime,
npy_datetimestruct,
Expand Down Expand Up @@ -249,6 +248,7 @@ cdef class DatetimeParseState:
# datetime.
self.found_naive_str = False
self.found_other = False

self.creso = creso
self.creso_ever_changed = False

Expand Down Expand Up @@ -305,7 +305,7 @@ def array_strptime(
fmt : string-like regex
exact : matches must be exact if True, search if False
errors : string specifying error handling, {'raise', 'ignore', 'coerce'}
creso : NPY_DATETIMEUNIT, default NPY_FR_ns
creso : NPY_DATETIMEUNIT, default NPY_FR_GENERIC
Set to NPY_FR_GENERIC to infer a resolution.
"""

Expand Down Expand Up @@ -627,7 +627,7 @@ cdef tzinfo _parse_with_format(
elif len(s) <= 6:
item_reso[0] = NPY_DATETIMEUNIT.NPY_FR_us
else:
item_reso[0] = NPY_FR_ns
item_reso[0] = NPY_DATETIMEUNIT.NPY_FR_ns
# Pad to always return nanoseconds
s += "0" * (9 - len(s))
us = long(s)
Expand Down
61 changes: 0 additions & 61 deletions pandas/tests/tslibs/test_array_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
tslib,
)
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

from pandas import Timestamp
import pandas._testing as tm
Expand Down Expand Up @@ -307,63 +306,3 @@ def test_datetime_subclass(data, expected):

expected = np.array(expected, dtype="M8[us]")
tm.assert_numpy_array_equal(result, expected)


class TestArrayToDatetimeResolutionInference:
# TODO: tests that include tzs, ints

def test_infer_homogeoneous_datetimes(self):
dt = datetime(2023, 10, 27, 18, 3, 5, 678000)
arr = np.array([dt, dt, dt], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array([dt, dt, dt], dtype="M8[us]")
tm.assert_numpy_array_equal(result, expected)

def test_infer_homogeoneous_date_objects(self):
dt = datetime(2023, 10, 27, 18, 3, 5, 678000)
dt2 = dt.date()
arr = np.array([None, dt2, dt2, dt2], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array([np.datetime64("NaT"), dt2, dt2, dt2], dtype="M8[s]")
tm.assert_numpy_array_equal(result, expected)

def test_infer_homogeoneous_dt64(self):
dt = datetime(2023, 10, 27, 18, 3, 5, 678000)
dt64 = np.datetime64(dt, "ms")
arr = np.array([None, dt64, dt64, dt64], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array([np.datetime64("NaT"), dt64, dt64, dt64], dtype="M8[ms]")
tm.assert_numpy_array_equal(result, expected)

def test_infer_homogeoneous_timestamps(self):
dt = datetime(2023, 10, 27, 18, 3, 5, 678000)
ts = Timestamp(dt).as_unit("ns")
arr = np.array([None, ts, ts, ts], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array([np.datetime64("NaT")] + [ts.asm8] * 3, dtype="M8[ns]")
tm.assert_numpy_array_equal(result, expected)

def test_infer_homogeoneous_datetimes_strings(self):
item = "2023-10-27 18:03:05.678000"
arr = np.array([None, item, item, item], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array([np.datetime64("NaT"), item, item, item], dtype="M8[us]")
tm.assert_numpy_array_equal(result, expected)

def test_infer_heterogeneous(self):
dtstr = "2023-10-27 18:03:05.678000"

arr = np.array([dtstr, dtstr[:-3], dtstr[:-7], None], dtype=object)
result, tz = tslib.array_to_datetime(arr, creso=creso_infer)
assert tz is None
expected = np.array(arr, dtype="M8[us]")
tm.assert_numpy_array_equal(result, expected)

result, tz = tslib.array_to_datetime(arr[::-1], creso=creso_infer)
assert tz is None
tm.assert_numpy_array_equal(result, expected[::-1])
18 changes: 8 additions & 10 deletions pandas/tests/util/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,14 @@ def test_categorical_consistency(s1, categorize):
tm.assert_series_equal(h1, h3)


def test_categorical_with_nan_consistency():
c = pd.Categorical.from_codes(
[-1, 0, 1, 2, 3, 4], categories=pd.date_range("2012-01-01", periods=5, name="B")
)
expected = hash_array(c, categorize=False)

c = pd.Categorical.from_codes(
[-1, 0], categories=[pd.Timestamp("2012-01-01").as_unit("ns")]
)
result = hash_array(c, categorize=False)
def test_categorical_with_nan_consistency(unit):
dti = pd.date_range("2012-01-01", periods=5, name="B", unit=unit)
cat = pd.Categorical.from_codes([-1, 0, 1, 2, 3, 4], categories=dti)
expected = hash_array(cat, categorize=False)

ts = pd.Timestamp("2012-01-01").as_unit(unit)
cat2 = pd.Categorical.from_codes([-1, 0], categories=[ts])
result = hash_array(cat2, categorize=False)

assert result[0] in expected
assert result[1] in expected
Expand Down

0 comments on commit 29854b7

Please sign in to comment.