Skip to content

Commit 225b9b0

Browse files
jbrockmendeljreback
authored andcommitted
CLN: de-duplicate generate_range (#23218)
1 parent c8b45e0 commit 225b9b0

File tree

7 files changed

+26
-41
lines changed

7 files changed

+26
-41
lines changed

pandas/core/arrays/datetimelike.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,12 @@ def _time_shift(self, periods, freq=None):
595595

596596
start = self[0] + periods * self.freq
597597
end = self[-1] + periods * self.freq
598-
attribs = self._get_attributes_dict()
598+
599+
# Note: in the DatetimeTZ case, _generate_range will infer the
600+
# appropriate timezone from `start` and `end`, so tz does not need
601+
# to be passed explicitly.
599602
return self._generate_range(start=start, end=end, periods=None,
600-
**attribs)
603+
freq=self.freq)
601604

602605
@classmethod
603606
def _add_datetimelike_methods(cls):

pandas/core/arrays/period.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ def _from_ordinals(cls, values, freq=None, **kwargs):
176176

177177
@classmethod
178178
def _generate_range(cls, start, end, periods, freq, fields):
179+
periods = dtl.validate_periods(periods)
180+
179181
if freq is not None:
180182
freq = Period._maybe_convert_freq(freq)
181183

182184
field_count = len(fields)
183-
if com.count_not_none(start, end) > 0:
185+
if start is not None or end is not None:
184186
if field_count > 0:
185187
raise ValueError('Can either instantiate from fields '
186188
'or endpoints, but not both')

pandas/core/arrays/timedeltas.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def _simple_new(cls, values, freq=None, **kwargs):
126126
result._freq = freq
127127
return result
128128

129-
def __new__(cls, values, freq=None, start=None, end=None, periods=None,
130-
closed=None):
129+
def __new__(cls, values, freq=None):
131130

132131
freq, freq_infer = dtl.maybe_infer_freq(freq)
133132

@@ -140,8 +139,7 @@ def __new__(cls, values, freq=None, start=None, end=None, periods=None,
140139
return result
141140

142141
@classmethod
143-
def _generate_range(cls, start, end, periods, freq, closed=None, **kwargs):
144-
# **kwargs are for compat with TimedeltaIndex, which includes `name`
142+
def _generate_range(cls, start, end, periods, freq, closed=None):
145143

146144
periods = dtl.validate_periods(periods)
147145
if freq is None and any(x is None for x in [periods, start, end]):
@@ -167,10 +165,9 @@ def _generate_range(cls, start, end, periods, freq, closed=None, **kwargs):
167165

168166
if freq is not None:
169167
index = _generate_regular_range(start, end, periods, freq)
170-
index = cls._simple_new(index, freq=freq, **kwargs)
168+
index = cls._simple_new(index, freq=freq)
171169
else:
172170
index = np.linspace(start.value, end.value, periods).astype('i8')
173-
# TODO: shouldn't we pass `name` here? (via **kwargs)
174171
index = cls._simple_new(index, freq=freq)
175172

176173
if not left_closed:

pandas/core/indexes/datetimelike.py

+6
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,12 @@ def astype(self, dtype, copy=True):
703703
raise TypeError(msg.format(name=type(self).__name__, dtype=dtype))
704704
return super(DatetimeIndexOpsMixin, self).astype(dtype, copy=copy)
705705

706+
@Appender(DatetimeLikeArrayMixin._time_shift.__doc__)
707+
def _time_shift(self, periods, freq=None):
708+
result = DatetimeLikeArrayMixin._time_shift(self, periods, freq=freq)
709+
result.name = self.name
710+
return result
711+
706712

707713
def _ensure_datetimelike_to_i8(other, to_utc=False):
708714
"""

pandas/core/indexes/datetimes.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,11 @@ def __new__(cls, data=None,
240240

241241
if data is None:
242242
# TODO: Remove this block and associated kwargs; GH#20535
243-
return cls._generate_range(start, end, periods, name, freq,
244-
tz=tz, normalize=normalize,
245-
closed=closed, ambiguous=ambiguous)
243+
result = cls._generate_range(start, end, periods,
244+
freq=freq, tz=tz, normalize=normalize,
245+
closed=closed, ambiguous=ambiguous)
246+
result.name = name
247+
return result
246248

247249
if not isinstance(data, (np.ndarray, Index, ABCSeries,
248250
DatetimeArrayMixin)):
@@ -314,17 +316,6 @@ def __new__(cls, data=None,
314316

315317
return subarr._deepcopy_if_needed(ref_to_data, copy)
316318

317-
@classmethod
318-
@Appender(DatetimeArrayMixin._generate_range.__doc__)
319-
def _generate_range(cls, start, end, periods, name=None, freq=None,
320-
tz=None, normalize=False, ambiguous='raise',
321-
closed=None):
322-
out = super(DatetimeIndex, cls)._generate_range(
323-
start, end, periods, freq,
324-
tz=tz, normalize=normalize, ambiguous=ambiguous, closed=closed)
325-
out.name = name
326-
return out
327-
328319
def _convert_for_op(self, value):
329320
""" Convert value to be insertable to ndarray """
330321
if self._has_same_tz(value):

pandas/core/indexes/period.py

-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
165165
raise TypeError('__new__() got an unexpected keyword argument {}'.
166166
format(list(set(fields) - valid_field_set)[0]))
167167

168-
periods = dtl.validate_periods(periods)
169-
170168
if name is None and hasattr(data, 'name'):
171169
name = data.name
172170

pandas/core/indexes/timedeltas.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,10 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
147147

148148
if data is None:
149149
# TODO: Remove this block and associated kwargs; GH#20535
150-
if freq is None and com._any_none(periods, start, end):
151-
raise ValueError('Must provide freq argument if no data is '
152-
'supplied')
153-
periods = dtl.validate_periods(periods)
154-
return cls._generate_range(start, end, periods, name, freq,
155-
closed=closed)
150+
result = cls._generate_range(start, end, periods, freq,
151+
closed=closed)
152+
result.name = name
153+
return result
156154

157155
if unit is not None:
158156
data = to_timedelta(data, unit=unit, box=False)
@@ -181,16 +179,6 @@ def __new__(cls, data=None, unit=None, freq=None, start=None, end=None,
181179

182180
return subarr
183181

184-
@classmethod
185-
def _generate_range(cls, start, end, periods,
186-
name=None, freq=None, closed=None):
187-
# TimedeltaArray gets `name` via **kwargs, so we need to explicitly
188-
# override it if name is passed as a positional argument
189-
return super(TimedeltaIndex, cls)._generate_range(start, end,
190-
periods, freq,
191-
name=name,
192-
closed=closed)
193-
194182
@classmethod
195183
def _simple_new(cls, values, name=None, freq=None, **kwargs):
196184
result = super(TimedeltaIndex, cls)._simple_new(values, freq, **kwargs)

0 commit comments

Comments
 (0)