Skip to content

Commit d867f16

Browse files
authored
TYP: tslib.pyi (#40473)
1 parent 88fb114 commit d867f16

File tree

11 files changed

+56
-23
lines changed

11 files changed

+56
-23
lines changed

pandas/_libs/tslib.pyi

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import tzinfo
2+
3+
import numpy as np
4+
5+
def format_array_from_datetime(
6+
values: np.ndarray, # np.ndarray[np.int64]
7+
tz: tzinfo | None = ...,
8+
format: str | None = ...,
9+
na_rep: object = ...
10+
) -> np.ndarray: ... # np.ndarray[object]
11+
12+
13+
def array_with_unit_to_datetime(
14+
values: np.ndarray,
15+
unit: str,
16+
errors: str = ...,
17+
) -> tuple[np.ndarray, tzinfo | None]: ...
18+
19+
20+
def array_to_datetime(
21+
values: np.ndarray, # np.ndarray[object]
22+
errors: str = ...,
23+
dayfirst: bool = ...,
24+
yearfirst: bool = ...,
25+
utc: bool = ...,
26+
require_iso8601: bool = ...,
27+
allow_mixed: bool = ...,
28+
) -> tuple[np.ndarray, tzinfo | None]: ...
29+
# returned ndarray may be object dtype or datetime64[ns]

pandas/_libs/tslib.pyx

+12-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def format_array_from_datetime(
100100
tzinfo tz=None,
101101
str format=None,
102102
object na_rep=None
103-
):
103+
) -> np.ndarray:
104104
"""
105105
return a np object array of the string formatted values
106106

@@ -113,6 +113,9 @@ def format_array_from_datetime(
113113
na_rep : optional, default is None
114114
a nat format
115115

116+
Returns
117+
-------
118+
np.ndarray[object]
116119
"""
117120
cdef:
118121
int64_t val, ns, N = len(values)
@@ -200,7 +203,7 @@ def array_with_unit_to_datetime(
200203
201204
Parameters
202205
----------
203-
values : ndarray of object
206+
values : ndarray
204207
Date-like objects to convert.
205208
unit : str
206209
Time unit to use during conversion.
@@ -411,7 +414,9 @@ cpdef array_to_datetime(
411414
412415
Returns
413416
-------
414-
tuple (ndarray, tzoffset)
417+
np.ndarray
418+
May be datetime64[ns] or object dtype
419+
tzinfo or None
415420
"""
416421
cdef:
417422
Py_ssize_t i, n = len(values)
@@ -635,7 +640,7 @@ cpdef array_to_datetime(
635640
return result, tz_out
636641

637642

638-
cdef ignore_errors_out_of_bounds_fallback(ndarray[object] values):
643+
cdef ndarray[object] ignore_errors_out_of_bounds_fallback(ndarray[object] values):
639644
"""
640645
Fallback for array_to_datetime if an OutOfBoundsDatetime is raised
641646
and errors == "ignore"
@@ -689,7 +694,7 @@ cdef _array_to_datetime_object(
689694
690695
Parameters
691696
----------
692-
values : ndarray of object
697+
values : ndarray[object]
693698
date-like objects to convert
694699
errors : str
695700
error behavior when parsing
@@ -700,7 +705,8 @@ cdef _array_to_datetime_object(
700705
701706
Returns
702707
-------
703-
tuple (ndarray, None)
708+
np.ndarray[object]
709+
Literal[None]
704710
"""
705711
cdef:
706712
Py_ssize_t i, n = len(values)

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ def objects_to_datetime64ns(
21542154
data = np.array(data, copy=False, dtype=np.object_)
21552155

21562156
flags = data.flags
2157-
order = "F" if flags.f_contiguous else "C"
2157+
order: Literal["F", "C"] = "F" if flags.f_contiguous else "C"
21582158
try:
21592159
result, tz_parsed = tslib.array_to_datetime(
21602160
data.ravel("K"),

pandas/core/tools/datetimes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ def _convert_listlike_datetimes(
307307
None or string for the Index name
308308
tz : object
309309
None or 'utc'
310-
unit : string
310+
unit : str
311311
None or string of the frequency of the passed data
312-
errors : string
312+
errors : str
313313
error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore'
314314
infer_datetime_format : bool, default False
315315
inferring format behavior from to_datetime
@@ -529,7 +529,7 @@ def _to_datetime_with_format(
529529
return result # type: ignore[return-value]
530530

531531

532-
def _to_datetime_with_unit(arg, unit, name, tz, errors: Optional[str]) -> Index:
532+
def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index:
533533
"""
534534
to_datetime specalized to the case where a 'unit' is passed.
535535
"""
@@ -1035,7 +1035,7 @@ def coerce(values):
10351035
return values
10361036

10371037

1038-
def _attempt_YYYYMMDD(arg: np.ndarray, errors: Optional[str]) -> Optional[np.ndarray]:
1038+
def _attempt_YYYYMMDD(arg: np.ndarray, errors: str) -> Optional[np.ndarray]:
10391039
"""
10401040
try to parse the YYYYMMDD/%Y%m%d format, try to deal with NaT-like,
10411041
arg is a passed in as an object dtype, but could really be ints/strings

pandas/tests/indexes/multi/test_constructors.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
import numpy as np
88
import pytest
99

10-
from pandas._libs.tslib import Timestamp
11-
1210
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
1311

1412
import pandas as pd
1513
from pandas import (
1614
Index,
1715
MultiIndex,
1816
Series,
17+
Timestamp,
1918
date_range,
2019
)
2120
import pandas._testing as tm

pandas/tests/indexes/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111
import pytest
1212

13-
from pandas._libs.tslib import Timestamp
1413
from pandas.compat import (
1514
IS64,
1615
np_datetime64_compat,
@@ -29,6 +28,7 @@
2928
RangeIndex,
3029
Series,
3130
TimedeltaIndex,
31+
Timestamp,
3232
UInt64Index,
3333
date_range,
3434
isna,

pandas/tests/io/json/test_ujson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import pytz
1515

1616
import pandas._libs.json as ujson
17-
from pandas._libs.tslib import Timestamp
1817
from pandas.compat import (
1918
IS64,
2019
is_platform_windows,
@@ -28,6 +27,7 @@
2827
NaT,
2928
Series,
3029
Timedelta,
30+
Timestamp,
3131
date_range,
3232
)
3333
import pandas._testing as tm

pandas/tests/io/parser/common/test_common_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import numpy as np
1212
import pytest
1313

14-
from pandas._libs.tslib import Timestamp
1514
from pandas.errors import (
1615
EmptyDataError,
1716
ParserError,
@@ -21,6 +20,7 @@
2120
DataFrame,
2221
Index,
2322
Series,
23+
Timestamp,
2424
compat,
2525
)
2626
import pandas._testing as tm

pandas/tests/io/parser/test_parse_dates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import pytest
2020
import pytz
2121

22-
from pandas._libs.tslib import Timestamp
2322
from pandas._libs.tslibs import parsing
2423
from pandas._libs.tslibs.parsing import parse_datetime_string
2524
from pandas.compat import (
@@ -34,6 +33,7 @@
3433
Index,
3534
MultiIndex,
3635
Series,
36+
Timestamp,
3737
)
3838
import pandas._testing as tm
3939
from pandas.core.indexes.datetimes import date_range

pandas/tests/io/parser/usecols/test_parse_dates.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
import pytest
88

9-
from pandas._libs.tslib import Timestamp
10-
119
from pandas import (
1210
DataFrame,
1311
Index,
12+
Timestamp,
1413
)
1514
import pandas._testing as tm
1615

pandas/tests/tools/test_to_datetime.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ def test_unit(self, cache):
11571157
tm.assert_index_equal(result, expected)
11581158

11591159
msg = "cannot convert input 11111111 with the unit 'D'"
1160-
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
1160+
with pytest.raises(OutOfBoundsDatetime, match=msg):
11611161
to_datetime(values, unit="D", errors="raise", cache=cache)
11621162

11631163
values = [1420043460000, iNaT, NaT, np.nan, "NaT"]
@@ -1171,15 +1171,15 @@ def test_unit(self, cache):
11711171
tm.assert_index_equal(result, expected)
11721172

11731173
msg = "cannot convert input 1420043460000 with the unit 's'"
1174-
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
1174+
with pytest.raises(OutOfBoundsDatetime, match=msg):
11751175
to_datetime(values, errors="raise", unit="s", cache=cache)
11761176

11771177
# if we have a string, then we raise a ValueError
11781178
# and NOT an OutOfBoundsDatetime
11791179
for val in ["foo", Timestamp("20130101")]:
11801180
try:
11811181
to_datetime(val, errors="raise", unit="s", cache=cache)
1182-
except tslib.OutOfBoundsDatetime as err:
1182+
except OutOfBoundsDatetime as err:
11831183
raise AssertionError("incorrect exception raised") from err
11841184
except ValueError:
11851185
pass
@@ -2347,7 +2347,7 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs):
23472347
("random_string", ValueError),
23482348
("epoch", ValueError),
23492349
("13-24-1990", ValueError),
2350-
(datetime(1, 1, 1), tslib.OutOfBoundsDatetime),
2350+
(datetime(1, 1, 1), OutOfBoundsDatetime),
23512351
],
23522352
)
23532353
def test_invalid_origins(self, origin, exc, units, units_from_epochs):

0 commit comments

Comments
 (0)