|
24 | 24 | Timedelta,
|
25 | 25 | Timestamp,
|
26 | 26 | iNaT,
|
27 |
| - nat_strings, |
28 |
| - parsing, |
29 | 27 | timezones as libtimezones,
|
30 | 28 | )
|
31 | 29 | from pandas._libs.tslibs.parsing import (
|
|
38 | 36 | AnyArrayLike,
|
39 | 37 | ArrayLike,
|
40 | 38 | DateTimeErrorChoices,
|
41 |
| - npt, |
42 | 39 | )
|
43 | 40 |
|
44 | 41 | from pandas.core.dtypes.common import (
|
|
57 | 54 | ABCDataFrame,
|
58 | 55 | ABCSeries,
|
59 | 56 | )
|
60 |
| -from pandas.core.dtypes.missing import notna |
61 | 57 |
|
62 | 58 | from pandas.arrays import (
|
63 | 59 | DatetimeArray,
|
64 | 60 | IntegerArray,
|
65 | 61 | )
|
66 |
| -from pandas.core import algorithms |
67 | 62 | from pandas.core.algorithms import unique
|
68 | 63 | from pandas.core.arrays.base import ExtensionArray
|
69 | 64 | from pandas.core.arrays.datetimes import (
|
@@ -407,7 +402,6 @@ def _convert_listlike_datetimes(
|
407 | 402 |
|
408 | 403 | # warn if passing timedelta64, raise for PeriodDtype
|
409 | 404 | # NB: this must come after unit transformation
|
410 |
| - orig_arg = arg |
411 | 405 | try:
|
412 | 406 | arg, _ = maybe_convert_dtype(arg, copy=False, tz=libtimezones.maybe_get_tz(tz))
|
413 | 407 | except TypeError:
|
@@ -435,8 +429,8 @@ def _convert_listlike_datetimes(
|
435 | 429 | require_iso8601 = not infer_datetime_format
|
436 | 430 |
|
437 | 431 | if format is not None and not require_iso8601:
|
438 |
| - res = _to_datetime_with_format( |
439 |
| - arg, orig_arg, name, utc, format, exact, errors, infer_datetime_format |
| 432 | + res = _array_strptime_with_fallback( |
| 433 | + arg, name, utc, format, exact, errors, infer_datetime_format |
440 | 434 | )
|
441 | 435 | if res is not None:
|
442 | 436 | return res
|
@@ -523,23 +517,6 @@ def _to_datetime_with_format(
|
523 | 517 | """
|
524 | 518 | Try parsing with the given format, returning None on failure.
|
525 | 519 | """
|
526 |
| - result = None |
527 |
| - |
528 |
| - # shortcut formatting here |
529 |
| - if fmt == "%Y%m%d": |
530 |
| - # pass orig_arg as float-dtype may have been converted to |
531 |
| - # datetime64[ns] |
532 |
| - orig_arg = ensure_object(orig_arg) |
533 |
| - try: |
534 |
| - # may return None without raising |
535 |
| - result = _attempt_YYYYMMDD(orig_arg, errors=errors) |
536 |
| - except (ValueError, TypeError, OutOfBoundsDatetime) as err: |
537 |
| - raise ValueError( |
538 |
| - "cannot convert the input to '%Y%m%d' date format" |
539 |
| - ) from err |
540 |
| - if result is not None: |
541 |
| - return _box_as_indexlike(result, utc=utc, name=name) |
542 |
| - |
543 | 520 | # fallback
|
544 | 521 | res = _array_strptime_with_fallback(
|
545 | 522 | arg, name, utc, fmt, exact, errors, infer_datetime_format
|
@@ -1244,60 +1221,6 @@ def coerce(values):
|
1244 | 1221 | return values
|
1245 | 1222 |
|
1246 | 1223 |
|
1247 |
| -def _attempt_YYYYMMDD(arg: npt.NDArray[np.object_], errors: str) -> np.ndarray | None: |
1248 |
| - """ |
1249 |
| - try to parse the YYYYMMDD/%Y%m%d format, try to deal with NaT-like, |
1250 |
| - arg is a passed in as an object dtype, but could really be ints/strings |
1251 |
| - with nan-like/or floats (e.g. with nan) |
1252 |
| -
|
1253 |
| - Parameters |
1254 |
| - ---------- |
1255 |
| - arg : np.ndarray[object] |
1256 |
| - errors : {'raise','ignore','coerce'} |
1257 |
| - """ |
1258 |
| - |
1259 |
| - def calc(carg): |
1260 |
| - # calculate the actual result |
1261 |
| - carg = carg.astype(object, copy=False) |
1262 |
| - parsed = parsing.try_parse_year_month_day( |
1263 |
| - carg / 10000, carg / 100 % 100, carg % 100 |
1264 |
| - ) |
1265 |
| - return tslib.array_to_datetime(parsed, errors=errors)[0] |
1266 |
| - |
1267 |
| - def calc_with_mask(carg, mask): |
1268 |
| - result = np.empty(carg.shape, dtype="M8[ns]") |
1269 |
| - iresult = result.view("i8") |
1270 |
| - iresult[~mask] = iNaT |
1271 |
| - |
1272 |
| - masked_result = calc(carg[mask].astype(np.float64).astype(np.int64)) |
1273 |
| - result[mask] = masked_result.astype("M8[ns]") |
1274 |
| - return result |
1275 |
| - |
1276 |
| - # try intlike / strings that are ints |
1277 |
| - try: |
1278 |
| - return calc(arg.astype(np.int64)) |
1279 |
| - except (ValueError, OverflowError, TypeError): |
1280 |
| - pass |
1281 |
| - |
1282 |
| - # a float with actual np.nan |
1283 |
| - try: |
1284 |
| - carg = arg.astype(np.float64) |
1285 |
| - return calc_with_mask(carg, notna(carg)) |
1286 |
| - except (ValueError, OverflowError, TypeError): |
1287 |
| - pass |
1288 |
| - |
1289 |
| - # string with NaN-like |
1290 |
| - try: |
1291 |
| - # error: Argument 2 to "isin" has incompatible type "List[Any]"; expected |
1292 |
| - # "Union[Union[ExtensionArray, ndarray], Index, Series]" |
1293 |
| - mask = ~algorithms.isin(arg, list(nat_strings)) # type: ignore[arg-type] |
1294 |
| - return calc_with_mask(arg, mask) |
1295 |
| - except (ValueError, OverflowError, TypeError): |
1296 |
| - pass |
1297 |
| - |
1298 |
| - return None |
1299 |
| - |
1300 |
| - |
1301 | 1224 | __all__ = [
|
1302 | 1225 | "DateParseError",
|
1303 | 1226 | "should_cache",
|
|
0 commit comments