Skip to content

Commit 5b12c36

Browse files
SNOW-1988858: Use docstrings folder for TimedeltaIndex methods and properties (#3176)
1 parent c21760e commit 5b12c36

File tree

3 files changed

+373
-295
lines changed

3 files changed

+373
-295
lines changed

src/snowflake/snowpark/modin/plugin/docstrings/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
CombinedDatetimelikeProperties,
2121
StringMethods,
2222
)
23+
from snowflake.snowpark.modin.plugin.docstrings.timedelta_index import TimedeltaIndex
2324
from snowflake.snowpark.modin.plugin.docstrings.window import Expanding, Rolling
2425

2526
__all__ = [
@@ -35,4 +36,5 @@
3536
"StringMethods",
3637
"Index",
3738
"DatetimeIndex",
39+
"TimedeltaIndex",
3840
]
Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
#
2+
# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved.
3+
#
4+
5+
# Licensed to Modin Development Team under one or more contributor license agreements.
6+
# See the NOTICE file distributed with this work for additional information regarding
7+
# copyright ownership. The Modin Development Team licenses this file to you under the
8+
# Apache License, Version 2.0 (the "License"); you may not use this file except in
9+
# compliance with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software distributed under
14+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
15+
# ANY KIND, either express or implied. See the License for the specific language
16+
# governing permissions and limitations under the License.
17+
18+
# Code in this file may constitute partial or total reimplementation, or modification of
19+
# existing code originally distributed by the Modin project, under the Apache License,
20+
# Version 2.0.
21+
22+
"""This module contains TimedeltaIndex docstrings that override modin's docstrings."""
23+
24+
from __future__ import annotations
25+
26+
from snowflake.snowpark.modin.plugin.extensions.index import Index
27+
28+
29+
class TimedeltaIndex(Index):
30+
def __new__():
31+
"""
32+
Create new instance of TimedeltaIndex. This overrides behavior of Index.__new__.
33+
34+
Parameters
35+
----------
36+
data : array-like (1-dimensional), optional
37+
Optional timedelta-like data to construct index with.
38+
unit : {'D', 'h', 'm', 's', 'ms', 'us', 'ns'}, optional
39+
The unit of ``data``.
40+
41+
.. deprecated:: 2.2.0
42+
Use ``pd.to_timedelta`` instead.
43+
44+
freq : str or pandas offset object, optional
45+
One of pandas date offset strings or corresponding objects. The string
46+
``'infer'`` can be passed in order to set the frequency of the index as
47+
the inferred frequency upon creation.
48+
dtype : numpy.dtype or str, default None
49+
Valid ``numpy`` dtypes are ``timedelta64[ns]``, ``timedelta64[us]``,
50+
``timedelta64[ms]``, and ``timedelta64[s]``.
51+
copy : bool
52+
Make a copy of input array.
53+
name : object
54+
Name to be stored in the index.
55+
56+
Returns:
57+
New instance of TimedeltaIndex.
58+
"""
59+
60+
def __init__() -> None:
61+
"""
62+
Immutable Index of timedelta64 data.
63+
64+
Represented internally as int64, and scalars returned Timedelta objects.
65+
66+
Parameters
67+
----------
68+
data : array-like (1-dimensional), optional
69+
Optional timedelta-like data to construct index with.
70+
unit : {'D', 'h', 'm', 's', 'ms', 'us', 'ns'}, optional
71+
The unit of ``data``.
72+
73+
.. deprecated:: 2.2.0
74+
Use ``pd.to_timedelta`` instead.
75+
76+
freq : str or pandas offset object, optional
77+
One of pandas date offset strings or corresponding objects. The string
78+
``'infer'`` can be passed in order to set the frequency of the index as
79+
the inferred frequency upon creation.
80+
dtype : numpy.dtype or str, default None
81+
Valid ``numpy`` dtypes are ``timedelta64[ns]``, ``timedelta64[us]``,
82+
``timedelta64[ms]``, and ``timedelta64[s]``.
83+
copy : bool
84+
Make a copy of input array.
85+
name : object
86+
Name to be stored in the index.
87+
88+
Examples
89+
--------
90+
>>> pd.TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'])
91+
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
92+
93+
We can also let pandas infer the frequency when possible.
94+
95+
>>> pd.TimedeltaIndex(np.arange(5) * 24 * 3600 * 1e9, freq='infer')
96+
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
97+
"""
98+
99+
@property
100+
def days():
101+
"""
102+
Number of days for each element.
103+
104+
Returns
105+
-------
106+
An Index with the days component of the timedelta.
107+
108+
Examples
109+
--------
110+
>>> idx = pd.to_timedelta(["0 days", "10 days", "20 days"])
111+
>>> idx
112+
TimedeltaIndex(['0 days', '10 days', '20 days'], dtype='timedelta64[ns]', freq=None)
113+
>>> idx.days
114+
Index([0, 10, 20], dtype='int64')
115+
"""
116+
117+
@property
118+
def seconds():
119+
"""
120+
Number of seconds (>= 0 and less than 1 day) for each element.
121+
122+
Returns
123+
-------
124+
An Index with seconds component of the timedelta.
125+
126+
Examples
127+
--------
128+
>>> idx = pd.to_timedelta([1, 2, 3], unit='s')
129+
>>> idx
130+
TimedeltaIndex(['0 days 00:00:01', '0 days 00:00:02', '0 days 00:00:03'], dtype='timedelta64[ns]', freq=None)
131+
>>> idx.seconds
132+
Index([1, 2, 3], dtype='int64')
133+
"""
134+
135+
@property
136+
def microseconds():
137+
"""
138+
Number of microseconds (>= 0 and less than 1 second) for each element.
139+
140+
Returns
141+
-------
142+
An Index with microseconds component of the timedelta.
143+
144+
Examples
145+
--------
146+
>>> idx = pd.to_timedelta([1, 2, 3], unit='us')
147+
>>> idx
148+
TimedeltaIndex(['0 days 00:00:00.000001', '0 days 00:00:00.000002',
149+
'0 days 00:00:00.000003'],
150+
dtype='timedelta64[ns]', freq=None)
151+
>>> idx.microseconds
152+
Index([1, 2, 3], dtype='int64')
153+
"""
154+
155+
@property
156+
def nanoseconds():
157+
"""
158+
Number of nonoseconds (>= 0 and less than 1 microsecond) for each element.
159+
160+
Returns
161+
-------
162+
An Index with nanoseconds compnent of the timedelta.
163+
164+
Examples
165+
--------
166+
>>> idx = pd.to_timedelta([1, 2, 3], unit='ns')
167+
>>> idx
168+
TimedeltaIndex(['0 days 00:00:00.000000001', '0 days 00:00:00.000000002',
169+
'0 days 00:00:00.000000003'],
170+
dtype='timedelta64[ns]', freq=None)
171+
>>> idx.nanoseconds
172+
Index([1, 2, 3], dtype='int64')
173+
"""
174+
175+
@property
176+
def components():
177+
"""
178+
Return a DataFrame of the individual resolution components of the Timedeltas.
179+
180+
The components (days, hours, minutes seconds, milliseconds, microseconds,
181+
nanoseconds) are returned as columns in a DataFrame.
182+
183+
Returns
184+
-------
185+
A DataFrame
186+
187+
Examples
188+
--------
189+
>>> idx = pd.to_timedelta(['1 day 3 min 2 us 42 ns']) # doctest: +SKIP
190+
>>> idx # doctest: +SKIP
191+
TimedeltaIndex(['1 days 00:03:00.000002042'],
192+
dtype='timedelta64[ns]', freq=None)
193+
>>> idx.components # doctest: +SKIP
194+
days hours minutes seconds milliseconds microseconds nanoseconds
195+
0 1 0 3 0 0 2 42
196+
"""
197+
198+
@property
199+
def inferred_freq():
200+
"""
201+
Tries to return a string representing a frequency generated by infer_freq.
202+
203+
Returns None if it can't autodetect the frequency.
204+
205+
Examples
206+
--------
207+
>>> idx = pd.to_timedelta(["0 days", "10 days", "20 days"]) # doctest: +SKIP
208+
>>> idx # doctest: +SKIP
209+
TimedeltaIndex(['0 days', '10 days', '20 days'],
210+
dtype='timedelta64[ns]', freq=None)
211+
>>> idx.inferred_freq # doctest: +SKIP
212+
'10D'
213+
"""
214+
215+
def round():
216+
"""
217+
Perform round operation on the data to the specified `freq`.
218+
219+
Parameters
220+
----------
221+
freq : str or Offset
222+
The frequency level to round the index to. Must be a fixed
223+
frequency like 'S' (second) not 'ME' (month end). See
224+
frequency aliases for a list of possible `freq` values.
225+
226+
Returns
227+
-------
228+
TimedeltaIndex with round values.
229+
230+
Raises
231+
------
232+
ValueError if the `freq` cannot be converted.
233+
"""
234+
235+
def floor():
236+
"""
237+
Perform floor operation on the data to the specified `freq`.
238+
239+
Parameters
240+
----------
241+
freq : str or Offset
242+
The frequency level to floor the index to. Must be a fixed
243+
frequency like 'S' (second) not 'ME' (month end). See
244+
frequency aliases for a list of possible `freq` values.
245+
246+
Returns
247+
-------
248+
TimedeltaIndex with floor values.
249+
250+
Raises
251+
------
252+
ValueError if the `freq` cannot be converted.
253+
"""
254+
255+
def ceil():
256+
"""
257+
Perform ceil operation on the data to the specified `freq`.
258+
259+
Parameters
260+
----------
261+
freq : str or Offset
262+
The frequency level to ceil the index to. Must be a fixed
263+
frequency like 'S' (second) not 'ME' (month end). See
264+
frequency aliases for a list of possible `freq` values.
265+
266+
Returns
267+
-------
268+
TimedeltaIndex with ceil values.
269+
270+
Raises
271+
------
272+
ValueError if the `freq` cannot be converted.
273+
"""
274+
275+
def to_pytimedelta():
276+
"""
277+
Return an ndarray of datetime.timedelta objects.
278+
279+
Returns
280+
-------
281+
numpy.ndarray
282+
283+
Examples
284+
--------
285+
>>> idx = pd.to_timedelta([1, 2, 3], unit='D') # doctest: +SKIP
286+
>>> idx # doctest: +SKIP
287+
TimedeltaIndex(['1 days', '2 days', '3 days'],
288+
dtype='timedelta64[ns]', freq=None)
289+
>>> idx.to_pytimedelta() # doctest: +SKIP
290+
array([datetime.timedelta(days=1), datetime.timedelta(days=2),
291+
datetime.timedelta(days=3)], dtype=object)
292+
"""
293+
294+
def mean():
295+
"""
296+
Return the mean value of the Timedelta values.
297+
298+
Parameters
299+
----------
300+
skipna : bool, default True
301+
Whether to ignore any NaT elements.
302+
axis : int, optional, default 0
303+
304+
Returns
305+
-------
306+
scalar Timedelta
307+
308+
Examples
309+
--------
310+
>>> idx = pd.to_timedelta([1, 2, 3, 1], unit='D')
311+
>>> idx
312+
TimedeltaIndex(['1 days', '2 days', '3 days', '1 days'], dtype='timedelta64[ns]', freq=None)
313+
>>> idx.mean()
314+
Timedelta('1 days 18:00:00')
315+
316+
See Also
317+
--------
318+
numpy.ndarray.mean : Returns the average of array elements along a given axis.
319+
Series.mean : Return the mean value in a Series.
320+
"""
321+
322+
def as_unit():
323+
"""
324+
Convert to a dtype with the given unit resolution.
325+
326+
Parameters
327+
----------
328+
unit : {'s', 'ms', 'us', 'ns'}
329+
330+
Returns
331+
-------
332+
DatetimeIndex
333+
334+
Examples
335+
--------
336+
>>> idx = pd.to_timedelta(['1 day 3 min 2 us 42 ns']) # doctest: +SKIP
337+
>>> idx # doctest: +SKIP
338+
TimedeltaIndex(['1 days 00:03:00.000002042'],
339+
dtype='timedelta64[ns]', freq=None)
340+
>>> idx.as_unit('s') # doctest: +SKIP
341+
TimedeltaIndex(['1 days 00:03:00'], dtype='timedelta64[s]', freq=None)
342+
"""
343+
344+
def total_seconds():
345+
"""
346+
Return total duration of each element expressed in seconds.
347+
348+
Returns
349+
-------
350+
An Index with float type.
351+
352+
Examples:
353+
--------
354+
>>> idx = pd.to_timedelta(np.arange(5), unit='d')
355+
>>> idx
356+
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
357+
>>> idx.total_seconds()
358+
Index([0.0, 86400.0, 172800.0, 259200.0, 345600.0], dtype='float64')
359+
"""

0 commit comments

Comments
 (0)