@@ -174,30 +174,22 @@ def detrend(x, key=None, axis=None):
174174 return detrend (x , key = detrend_linear , axis = axis )
175175 elif key == 'none' :
176176 return detrend (x , key = detrend_none , axis = axis )
177- elif isinstance (key , str ):
178- raise ValueError ("Unknown value for key %s, must be one of: "
179- "'default', 'constant', 'mean', "
180- "'linear', or a function" % key )
181-
182- if not callable (key ):
183- raise ValueError ("Unknown value for key %s, must be one of: "
184- "'default', 'constant', 'mean', "
185- "'linear', or a function" % key )
186-
187- x = np .asarray (x )
188-
189- if axis is not None and axis + 1 > x .ndim :
190- raise ValueError ('axis(=%s) out of bounds' % axis )
191-
192- if (axis is None and x .ndim == 0 ) or (not axis and x .ndim == 1 ):
193- return key (x )
194-
195- # try to use the 'axis' argument if the function supports it,
196- # otherwise use apply_along_axis to do it
197- try :
198- return key (x , axis = axis )
199- except TypeError :
200- return np .apply_along_axis (key , axis = axis , arr = x )
177+ elif callable (key ):
178+ x = np .asarray (x )
179+ if axis is not None and axis + 1 > x .ndim :
180+ raise ValueError (f'axis(={ axis } ) out of bounds' )
181+ if (axis is None and x .ndim == 0 ) or (not axis and x .ndim == 1 ):
182+ return key (x )
183+ # try to use the 'axis' argument if the function supports it,
184+ # otherwise use apply_along_axis to do it
185+ try :
186+ return key (x , axis = axis )
187+ except TypeError :
188+ return np .apply_along_axis (key , axis = axis , arr = x )
189+ else :
190+ raise ValueError (
191+ f"Unknown value for key: { key !r} , must be one of: 'default', "
192+ f"'constant', 'mean', 'linear', or a function" )
201193
202194
203195@cbook .deprecated ("3.1" , alternative = "detrend_mean" )
0 commit comments