1
1
# TODO: Use the fact that axis can have units to simplify the process
2
2
3
3
import functools
4
- from typing import Optional
4
+ from typing import TYPE_CHECKING , Optional
5
5
6
6
import numpy as np
7
7
20
20
TimeSeries_DateLocator ,
21
21
TimeSeries_TimedeltaFormatter ,
22
22
)
23
- import pandas .tseries .frequencies as frequencies
24
- from pandas .tseries .frequencies import is_subperiod , is_superperiod
23
+ from pandas .tseries .frequencies import (
24
+ get_period_alias ,
25
+ is_subperiod ,
26
+ is_superperiod ,
27
+ to_offset ,
28
+ )
25
29
from pandas .tseries .offsets import DateOffset
26
30
31
+ if TYPE_CHECKING :
32
+ from pandas import Series , Index # noqa:F401
33
+
34
+
27
35
# ---------------------------------------------------------------------
28
36
# Plotting functions and monkey patches
29
37
30
38
31
- def _maybe_resample (series , ax , kwargs ):
39
+ def _maybe_resample (series : "Series" , ax , kwargs ):
32
40
# resample against axes freq if necessary
33
41
freq , ax_freq = _get_freq (ax , series )
34
42
@@ -42,7 +50,7 @@ def _maybe_resample(series, ax, kwargs):
42
50
if ax_freq is not None and freq != ax_freq :
43
51
if is_superperiod (freq , ax_freq ): # upsample input
44
52
series = series .copy ()
45
- series .index = series .index .asfreq (ax_freq , how = "s" )
53
+ series .index = series .index .asfreq (ax_freq , how = "s" ) # type: ignore
46
54
freq = ax_freq
47
55
elif _is_sup (freq , ax_freq ): # one is weekly
48
56
how = kwargs .pop ("how" , "last" )
@@ -161,21 +169,22 @@ def _get_ax_freq(ax):
161
169
return ax_freq
162
170
163
171
164
- def get_period_alias (freq ) -> Optional [str ]:
172
+ def _get_period_alias (freq ) -> Optional [str ]:
165
173
if isinstance (freq , DateOffset ):
166
174
freq = freq .rule_code
167
175
else :
168
176
freq = base_and_stride (freq )[0 ]
169
177
170
- freq = frequencies . get_period_alias (freq )
178
+ freq = get_period_alias (freq )
171
179
return freq
172
180
173
181
174
- def _get_freq (ax , series ):
182
+ def _get_freq (ax , series : "Series" ):
175
183
# get frequency from data
176
184
freq = getattr (series .index , "freq" , None )
177
185
if freq is None :
178
186
freq = getattr (series .index , "inferred_freq" , None )
187
+ freq = to_offset (freq )
179
188
180
189
ax_freq = _get_ax_freq (ax )
181
190
@@ -184,12 +193,12 @@ def _get_freq(ax, series):
184
193
freq = ax_freq
185
194
186
195
# get the period frequency
187
- freq = get_period_alias (freq )
196
+ freq = _get_period_alias (freq )
188
197
return freq , ax_freq
189
198
190
199
191
200
def _use_dynamic_x (ax , data ):
192
- freq = _get_index_freq (data )
201
+ freq = _get_index_freq (data . index )
193
202
ax_freq = _get_ax_freq (ax )
194
203
195
204
if freq is None : # convert irregular if axes has freq info
@@ -201,7 +210,7 @@ def _use_dynamic_x(ax, data):
201
210
if freq is None :
202
211
return False
203
212
204
- freq = get_period_alias (freq )
213
+ freq = _get_period_alias (freq )
205
214
206
215
if freq is None :
207
216
return False
@@ -216,33 +225,37 @@ def _use_dynamic_x(ax, data):
216
225
return True
217
226
218
227
219
- def _get_index_freq (data ) :
220
- freq = getattr (data . index , "freq" , None )
228
+ def _get_index_freq (index : "Index" ) -> Optional [ DateOffset ] :
229
+ freq = getattr (index , "freq" , None )
221
230
if freq is None :
222
- freq = getattr (data . index , "inferred_freq" , None )
231
+ freq = getattr (index , "inferred_freq" , None )
223
232
if freq == "B" :
224
- weekdays = np .unique (data . index .dayofweek )
233
+ weekdays = np .unique (index .dayofweek ) # type: ignore
225
234
if (5 in weekdays ) or (6 in weekdays ):
226
235
freq = None
236
+
237
+ freq = to_offset (freq )
227
238
return freq
228
239
229
240
230
241
def _maybe_convert_index (ax , data ):
231
242
# tsplot converts automatically, but don't want to convert index
232
243
# over and over for DataFrames
233
244
if isinstance (data .index , (ABCDatetimeIndex , ABCPeriodIndex )):
234
- freq = getattr ( data .index , " freq" , None )
245
+ freq = data .index . freq
235
246
236
247
if freq is None :
237
- freq = getattr (data .index , "inferred_freq" , None )
248
+ # We only get here for DatetimeIndex
249
+ freq = data .index .inferred_freq
250
+ freq = to_offset (freq )
238
251
239
252
if freq is None :
240
253
freq = _get_ax_freq (ax )
241
254
242
255
if freq is None :
243
256
raise ValueError ("Could not get frequency alias for plotting" )
244
257
245
- freq = get_period_alias (freq )
258
+ freq = _get_period_alias (freq )
246
259
247
260
if isinstance (data .index , ABCDatetimeIndex ):
248
261
data = data .tz_localize (None ).to_period (freq = freq )
0 commit comments