Skip to content

Commit 2ba5426

Browse files
committed
CLN: tslibs typing, docstrings
1 parent 8376067 commit 2ba5426

File tree

7 files changed

+48
-42
lines changed

7 files changed

+48
-42
lines changed

pandas/_libs/tslib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def ints_to_pydatetime(const int64_t[:] arr, object tz=None, object freq=None,
188188
return result
189189

190190

191-
def _test_parse_iso8601(object ts):
191+
def _test_parse_iso8601(ts: str):
192192
"""
193193
TESTING ONLY: Parse string into Timestamp using iso8601 parser. Used
194194
only for testing, actual construction uses `convert_str_to_tsobject`

pandas/_libs/tslibs/conversion.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,15 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
444444
bint dayfirst=False,
445445
bint yearfirst=False):
446446
"""
447-
Convert a string-like (bytes or unicode) input `ts`, along with optional
448-
timezone object `tz` to a _TSObject.
447+
Convert a string input `ts`, along with optional timezone object`tz`
448+
to a _TSObject.
449449
450450
The optional arguments `dayfirst` and `yearfirst` are passed to the
451451
dateutil parser.
452452
453453
Parameters
454454
----------
455-
ts : bytes or unicode
455+
ts : str
456456
Value to be converted to _TSObject
457457
tz : tzinfo or None
458458
timezone for the timezone-aware output

pandas/_libs/tslibs/np_datetime.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ cdef npy_datetime get_datetime64_value(object obj) nogil
7272
cdef npy_timedelta get_timedelta64_value(object obj) nogil
7373
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil
7474

75-
cdef int _string_to_dts(object val, npy_datetimestruct* dts,
75+
cdef int _string_to_dts(str val, npy_datetimestruct* dts,
7676
int* out_local, int* out_tzoffset,
7777
bint want_exc) except? -1

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ cdef inline int64_t pydate_to_dt64(date val, npy_datetimestruct *dts):
167167
return dtstruct_to_dt64(dts)
168168

169169

170-
cdef inline int _string_to_dts(object val, npy_datetimestruct* dts,
170+
cdef inline int _string_to_dts(str val, npy_datetimestruct* dts,
171171
int* out_local, int* out_tzoffset,
172172
bint want_exc) except? -1:
173173
cdef:

pandas/_libs/tslibs/parsing.pyx

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,15 @@ cdef inline int _parse_4digit(const char* s):
8686
return result
8787

8888

89-
cdef inline object _parse_delimited_date(object date_string, bint dayfirst):
89+
cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
9090
"""
9191
Parse special cases of dates: MM/DD/YYYY, DD/MM/YYYY, MM/YYYY.
92+
9293
At the beginning function tries to parse date in MM/DD/YYYY format, but
9394
if month > 12 - in DD/MM/YYYY (`dayfirst == False`).
9495
With `dayfirst == True` function makes an attempt to parse date in
9596
DD/MM/YYYY, if an attempt is wrong - in DD/MM/YYYY
9697
97-
Note
98-
----
9998
For MM/DD/YYYY, DD/MM/YYYY: delimiter can be a space or one of /-.
10099
For MM/YYYY: delimiter can be a space or one of /-
101100
If `date_string` can't be converted to date, then function returns
@@ -104,11 +103,13 @@ cdef inline object _parse_delimited_date(object date_string, bint dayfirst):
104103
Parameters
105104
----------
106105
date_string : str
107-
dayfirst : bint
106+
dayfirst : bool
108107
109108
Returns:
110109
--------
111-
datetime, resolution
110+
datetime or Nont
111+
str or None
112+
Describing resolution of the parsed string.
112113
"""
113114
cdef:
114115
const char* buf
@@ -156,18 +157,19 @@ cdef inline object _parse_delimited_date(object date_string, bint dayfirst):
156157
raise DateParseError(f"Invalid date specified ({month}/{day})")
157158

158159

159-
cdef inline bint does_string_look_like_time(object parse_string):
160+
cdef inline bint does_string_look_like_time(str parse_string):
160161
"""
161162
Checks whether given string is a time: it has to start either from
162163
H:MM or from HH:MM, and hour and minute values must be valid.
163164
164165
Parameters
165166
----------
166-
date_string : str
167+
parse_string : str
167168
168169
Returns:
169170
--------
170-
whether given string is a time
171+
bool
172+
Whether given string is potentially a time.
171173
"""
172174
cdef:
173175
const char* buf
@@ -188,9 +190,10 @@ cdef inline bint does_string_look_like_time(object parse_string):
188190
return 0 <= hour <= 23 and 0 <= minute <= 59
189191

190192

191-
def parse_datetime_string(date_string, freq=None, dayfirst=False,
193+
def parse_datetime_string(date_string: str, freq=None, dayfirst=False,
192194
yearfirst=False, **kwargs):
193-
"""parse datetime string, only returns datetime.
195+
"""
196+
Parse datetime string, only returns datetime.
194197
Also cares special handling matching time patterns.
195198
196199
Returns
@@ -270,16 +273,17 @@ def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):
270273
return res
271274

272275

273-
cdef parse_datetime_string_with_reso(date_string, freq=None, dayfirst=False,
276+
cdef parse_datetime_string_with_reso(str date_string, freq=None, dayfirst=False,
274277
yearfirst=False):
275-
"""parse datetime string, only returns datetime
278+
"""
279+
Parse datetime string and try to identify its resolution.
276280
277281
Returns
278282
-------
279-
parsed : datetime
280-
parsed2 : datetime/dateutil.parser._result
281-
reso : str
282-
inferred resolution
283+
datetime
284+
datetime/dateutil.parser._result
285+
str
286+
Inferred resolution of the parsed string.
283287
284288
Raises
285289
------
@@ -315,18 +319,19 @@ cdef parse_datetime_string_with_reso(date_string, freq=None, dayfirst=False,
315319
return parsed, parsed, reso
316320

317321

318-
cpdef bint _does_string_look_like_datetime(object py_string):
322+
cpdef bint _does_string_look_like_datetime(str py_string):
319323
"""
320324
Checks whether given string is a datetime: it has to start with '0' or
321325
be greater than 1000.
322326
323327
Parameters
324328
----------
325-
py_string: object
329+
py_string: str
326330
327331
Returns
328332
-------
329-
whether given string is a datetime
333+
bool
334+
Whether given string is potentially a datetime.
330335
"""
331336
cdef:
332337
const char *buf
@@ -370,9 +375,6 @@ cdef inline object _parse_dateabbr_string(object date_string, object default,
370375
# special handling for possibilities eg, 2Q2005, 2Q05, 2005Q1, 05Q1
371376
assert isinstance(date_string, str)
372377

373-
# len(date_string) == 0
374-
# should be NaT???
375-
376378
if date_string in nat_strings:
377379
return NaT, NaT, ''
378380

@@ -530,7 +532,7 @@ cdef dateutil_parse(object timestr, object default, ignoretz=False,
530532
return ret, reso
531533

532534

533-
cdef object _get_rule_month(object source, object default='DEC'):
535+
cdef str _get_rule_month(object source):
534536
"""
535537
Return starting month of given freq, default is December.
536538
@@ -546,7 +548,8 @@ cdef object _get_rule_month(object source, object default='DEC'):
546548
source = source.freqstr
547549
source = source.upper()
548550
if '-' not in source:
549-
return default
551+
# Default is December
552+
return "DEC"
550553
else:
551554
return source.split('-')[1]
552555

@@ -939,14 +942,14 @@ def _concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
939942
940943
Parameters
941944
----------
942-
date_cols : tuple of numpy arrays
945+
date_cols : tuple[ndarray]
943946
keep_trivial_numbers : bool, default True
944947
if True and len(date_cols) == 1, then
945948
conversion (to string from integer/float zero) is not performed
946949
947950
Returns
948951
-------
949-
arr_of_rows : ndarray (dtype=object)
952+
arr_of_rows : ndarray[object]
950953
951954
Examples
952955
--------

pandas/_libs/tslibs/period.pyx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,12 +1191,15 @@ cdef int64_t period_ordinal_to_dt64(int64_t ordinal, int freq) except? -1:
11911191
return dtstruct_to_dt64(&dts)
11921192

11931193

1194-
def period_format(int64_t value, int freq, object fmt=None):
1194+
cdef str period_format(int64_t value, int freq, object fmt=None):
11951195
cdef:
11961196
int freq_group
11971197

11981198
if value == NPY_NAT:
1199-
return repr(NaT)
1199+
return "NaT"
1200+
1201+
if isinstance(fmt, str):
1202+
fmt = fmt.encode("utf-8")
12001203

12011204
if fmt is None:
12021205
freq_group = get_freq_group(freq)
@@ -1242,24 +1245,22 @@ cdef list extra_fmts = [(b"%q", b"^`AB`^"),
12421245
cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
12431246
"^`GH`^", "^`IJ`^", "^`KL`^"]
12441247

1245-
cdef object _period_strftime(int64_t value, int freq, object fmt):
1248+
cdef str _period_strftime(int64_t value, int freq, bytes fmt):
12461249
cdef:
12471250
Py_ssize_t i
12481251
npy_datetimestruct dts
12491252
char *formatted
1250-
object pat, repl, result
1253+
bytes pat, brepl
12511254
list found_pat = [False] * len(extra_fmts)
12521255
int year, quarter
1253-
1254-
if isinstance(fmt, unicode):
1255-
fmt = fmt.encode('utf-8')
1256+
str result, repl
12561257

12571258
get_date_info(value, freq, &dts)
12581259
for i in range(len(extra_fmts)):
12591260
pat = extra_fmts[i][0]
1260-
repl = extra_fmts[i][1]
1261+
brepl = extra_fmts[i][1]
12611262
if pat in fmt:
1262-
fmt = fmt.replace(pat, repl)
1263+
fmt = fmt.replace(pat, brepl)
12631264
found_pat[i] = True
12641265

12651266
formatted = c_strftime(&dts, <char*>fmt)
@@ -2234,7 +2235,7 @@ cdef class _Period:
22342235
object_state = None, self.freq, self.ordinal
22352236
return (Period, object_state)
22362237

2237-
def strftime(self, fmt):
2238+
def strftime(self, fmt: str) -> str:
22382239
"""
22392240
Returns the string representation of the :class:`Period`, depending
22402241
on the selected ``fmt``. ``fmt`` must be a string

pandas/plotting/_matplotlib/converter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,8 @@ def __call__(self, x, pos=0):
10971097
return ""
10981098
else:
10991099
fmt = self.formatdict.pop(x, "")
1100+
if isinstance(fmt, np.bytes_):
1101+
fmt = fmt.decode("utf-8")
11001102
return Period(ordinal=int(x), freq=self.freq).strftime(fmt)
11011103

11021104

0 commit comments

Comments
 (0)