polars-runtime-compat 1.34.0b3__cp39-abi3-win_amd64.whl → 1.34.0b5__cp39-abi3-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of polars-runtime-compat might be problematic. Click here for more details.

Files changed (204) hide show
  1. _polars_runtime_compat/_polars_runtime_compat.pyd +0 -0
  2. polars_runtime_compat-1.34.0b5.dist-info/METADATA +35 -0
  3. polars_runtime_compat-1.34.0b5.dist-info/RECORD +6 -0
  4. polars/__init__.py +0 -528
  5. polars/_cpu_check.py +0 -265
  6. polars/_dependencies.py +0 -355
  7. polars/_plr.py +0 -99
  8. polars/_plr.pyi +0 -2496
  9. polars/_reexport.py +0 -23
  10. polars/_typing.py +0 -478
  11. polars/_utils/__init__.py +0 -37
  12. polars/_utils/async_.py +0 -102
  13. polars/_utils/cache.py +0 -176
  14. polars/_utils/cloud.py +0 -40
  15. polars/_utils/constants.py +0 -29
  16. polars/_utils/construction/__init__.py +0 -46
  17. polars/_utils/construction/dataframe.py +0 -1397
  18. polars/_utils/construction/other.py +0 -72
  19. polars/_utils/construction/series.py +0 -560
  20. polars/_utils/construction/utils.py +0 -118
  21. polars/_utils/convert.py +0 -224
  22. polars/_utils/deprecation.py +0 -406
  23. polars/_utils/getitem.py +0 -457
  24. polars/_utils/logging.py +0 -11
  25. polars/_utils/nest_asyncio.py +0 -264
  26. polars/_utils/parquet.py +0 -15
  27. polars/_utils/parse/__init__.py +0 -12
  28. polars/_utils/parse/expr.py +0 -242
  29. polars/_utils/polars_version.py +0 -19
  30. polars/_utils/pycapsule.py +0 -53
  31. polars/_utils/scan.py +0 -27
  32. polars/_utils/serde.py +0 -63
  33. polars/_utils/slice.py +0 -215
  34. polars/_utils/udfs.py +0 -1251
  35. polars/_utils/unstable.py +0 -63
  36. polars/_utils/various.py +0 -782
  37. polars/_utils/wrap.py +0 -25
  38. polars/api.py +0 -370
  39. polars/catalog/__init__.py +0 -0
  40. polars/catalog/unity/__init__.py +0 -19
  41. polars/catalog/unity/client.py +0 -733
  42. polars/catalog/unity/models.py +0 -152
  43. polars/config.py +0 -1571
  44. polars/convert/__init__.py +0 -25
  45. polars/convert/general.py +0 -1046
  46. polars/convert/normalize.py +0 -261
  47. polars/dataframe/__init__.py +0 -5
  48. polars/dataframe/_html.py +0 -186
  49. polars/dataframe/frame.py +0 -12582
  50. polars/dataframe/group_by.py +0 -1067
  51. polars/dataframe/plotting.py +0 -257
  52. polars/datatype_expr/__init__.py +0 -5
  53. polars/datatype_expr/array.py +0 -56
  54. polars/datatype_expr/datatype_expr.py +0 -304
  55. polars/datatype_expr/list.py +0 -18
  56. polars/datatype_expr/struct.py +0 -69
  57. polars/datatypes/__init__.py +0 -122
  58. polars/datatypes/_parse.py +0 -195
  59. polars/datatypes/_utils.py +0 -48
  60. polars/datatypes/classes.py +0 -1213
  61. polars/datatypes/constants.py +0 -11
  62. polars/datatypes/constructor.py +0 -172
  63. polars/datatypes/convert.py +0 -366
  64. polars/datatypes/group.py +0 -130
  65. polars/exceptions.py +0 -230
  66. polars/expr/__init__.py +0 -7
  67. polars/expr/array.py +0 -964
  68. polars/expr/binary.py +0 -346
  69. polars/expr/categorical.py +0 -306
  70. polars/expr/datetime.py +0 -2620
  71. polars/expr/expr.py +0 -11272
  72. polars/expr/list.py +0 -1408
  73. polars/expr/meta.py +0 -444
  74. polars/expr/name.py +0 -321
  75. polars/expr/string.py +0 -3045
  76. polars/expr/struct.py +0 -357
  77. polars/expr/whenthen.py +0 -185
  78. polars/functions/__init__.py +0 -193
  79. polars/functions/aggregation/__init__.py +0 -33
  80. polars/functions/aggregation/horizontal.py +0 -298
  81. polars/functions/aggregation/vertical.py +0 -341
  82. polars/functions/as_datatype.py +0 -848
  83. polars/functions/business.py +0 -138
  84. polars/functions/col.py +0 -384
  85. polars/functions/datatype.py +0 -121
  86. polars/functions/eager.py +0 -524
  87. polars/functions/escape_regex.py +0 -29
  88. polars/functions/lazy.py +0 -2751
  89. polars/functions/len.py +0 -68
  90. polars/functions/lit.py +0 -210
  91. polars/functions/random.py +0 -22
  92. polars/functions/range/__init__.py +0 -19
  93. polars/functions/range/_utils.py +0 -15
  94. polars/functions/range/date_range.py +0 -303
  95. polars/functions/range/datetime_range.py +0 -370
  96. polars/functions/range/int_range.py +0 -348
  97. polars/functions/range/linear_space.py +0 -311
  98. polars/functions/range/time_range.py +0 -287
  99. polars/functions/repeat.py +0 -301
  100. polars/functions/whenthen.py +0 -353
  101. polars/interchange/__init__.py +0 -10
  102. polars/interchange/buffer.py +0 -77
  103. polars/interchange/column.py +0 -190
  104. polars/interchange/dataframe.py +0 -230
  105. polars/interchange/from_dataframe.py +0 -328
  106. polars/interchange/protocol.py +0 -303
  107. polars/interchange/utils.py +0 -170
  108. polars/io/__init__.py +0 -64
  109. polars/io/_utils.py +0 -317
  110. polars/io/avro.py +0 -49
  111. polars/io/clipboard.py +0 -36
  112. polars/io/cloud/__init__.py +0 -17
  113. polars/io/cloud/_utils.py +0 -80
  114. polars/io/cloud/credential_provider/__init__.py +0 -17
  115. polars/io/cloud/credential_provider/_builder.py +0 -520
  116. polars/io/cloud/credential_provider/_providers.py +0 -618
  117. polars/io/csv/__init__.py +0 -9
  118. polars/io/csv/_utils.py +0 -38
  119. polars/io/csv/batched_reader.py +0 -142
  120. polars/io/csv/functions.py +0 -1495
  121. polars/io/database/__init__.py +0 -6
  122. polars/io/database/_arrow_registry.py +0 -70
  123. polars/io/database/_cursor_proxies.py +0 -147
  124. polars/io/database/_executor.py +0 -578
  125. polars/io/database/_inference.py +0 -314
  126. polars/io/database/_utils.py +0 -144
  127. polars/io/database/functions.py +0 -516
  128. polars/io/delta.py +0 -499
  129. polars/io/iceberg/__init__.py +0 -3
  130. polars/io/iceberg/_utils.py +0 -697
  131. polars/io/iceberg/dataset.py +0 -556
  132. polars/io/iceberg/functions.py +0 -151
  133. polars/io/ipc/__init__.py +0 -8
  134. polars/io/ipc/functions.py +0 -514
  135. polars/io/json/__init__.py +0 -3
  136. polars/io/json/read.py +0 -101
  137. polars/io/ndjson.py +0 -332
  138. polars/io/parquet/__init__.py +0 -17
  139. polars/io/parquet/field_overwrites.py +0 -140
  140. polars/io/parquet/functions.py +0 -722
  141. polars/io/partition.py +0 -491
  142. polars/io/plugins.py +0 -187
  143. polars/io/pyarrow_dataset/__init__.py +0 -5
  144. polars/io/pyarrow_dataset/anonymous_scan.py +0 -109
  145. polars/io/pyarrow_dataset/functions.py +0 -79
  146. polars/io/scan_options/__init__.py +0 -5
  147. polars/io/scan_options/_options.py +0 -59
  148. polars/io/scan_options/cast_options.py +0 -126
  149. polars/io/spreadsheet/__init__.py +0 -6
  150. polars/io/spreadsheet/_utils.py +0 -52
  151. polars/io/spreadsheet/_write_utils.py +0 -647
  152. polars/io/spreadsheet/functions.py +0 -1323
  153. polars/lazyframe/__init__.py +0 -9
  154. polars/lazyframe/engine_config.py +0 -61
  155. polars/lazyframe/frame.py +0 -8564
  156. polars/lazyframe/group_by.py +0 -669
  157. polars/lazyframe/in_process.py +0 -42
  158. polars/lazyframe/opt_flags.py +0 -333
  159. polars/meta/__init__.py +0 -14
  160. polars/meta/build.py +0 -33
  161. polars/meta/index_type.py +0 -27
  162. polars/meta/thread_pool.py +0 -50
  163. polars/meta/versions.py +0 -120
  164. polars/ml/__init__.py +0 -0
  165. polars/ml/torch.py +0 -213
  166. polars/ml/utilities.py +0 -30
  167. polars/plugins.py +0 -155
  168. polars/py.typed +0 -0
  169. polars/pyproject.toml +0 -103
  170. polars/schema.py +0 -265
  171. polars/selectors.py +0 -3117
  172. polars/series/__init__.py +0 -5
  173. polars/series/array.py +0 -776
  174. polars/series/binary.py +0 -254
  175. polars/series/categorical.py +0 -246
  176. polars/series/datetime.py +0 -2275
  177. polars/series/list.py +0 -1087
  178. polars/series/plotting.py +0 -191
  179. polars/series/series.py +0 -9197
  180. polars/series/string.py +0 -2367
  181. polars/series/struct.py +0 -154
  182. polars/series/utils.py +0 -191
  183. polars/sql/__init__.py +0 -7
  184. polars/sql/context.py +0 -677
  185. polars/sql/functions.py +0 -139
  186. polars/string_cache.py +0 -185
  187. polars/testing/__init__.py +0 -13
  188. polars/testing/asserts/__init__.py +0 -9
  189. polars/testing/asserts/frame.py +0 -231
  190. polars/testing/asserts/series.py +0 -219
  191. polars/testing/asserts/utils.py +0 -12
  192. polars/testing/parametric/__init__.py +0 -33
  193. polars/testing/parametric/profiles.py +0 -107
  194. polars/testing/parametric/strategies/__init__.py +0 -22
  195. polars/testing/parametric/strategies/_utils.py +0 -14
  196. polars/testing/parametric/strategies/core.py +0 -615
  197. polars/testing/parametric/strategies/data.py +0 -452
  198. polars/testing/parametric/strategies/dtype.py +0 -436
  199. polars/testing/parametric/strategies/legacy.py +0 -169
  200. polars/type_aliases.py +0 -24
  201. polars_runtime_compat-1.34.0b3.dist-info/METADATA +0 -190
  202. polars_runtime_compat-1.34.0b3.dist-info/RECORD +0 -203
  203. {polars_runtime_compat-1.34.0b3.dist-info → polars_runtime_compat-1.34.0b5.dist-info}/WHEEL +0 -0
  204. {polars_runtime_compat-1.34.0b3.dist-info → polars_runtime_compat-1.34.0b5.dist-info}/licenses/LICENSE +0 -0
polars/series/datetime.py DELETED
@@ -1,2275 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from typing import TYPE_CHECKING
4
-
5
- from polars._utils.deprecation import deprecate_nonkeyword_arguments, deprecated
6
- from polars._utils.unstable import unstable
7
- from polars._utils.wrap import wrap_s
8
- from polars.series.utils import expr_dispatch
9
-
10
- if TYPE_CHECKING:
11
- import datetime as dt
12
- import sys
13
- from collections.abc import Iterable
14
-
15
- from polars import Series
16
- from polars._plr import PySeries
17
- from polars._typing import (
18
- Ambiguous,
19
- EpochTimeUnit,
20
- IntoExpr,
21
- IntoExprColumn,
22
- NonExistent,
23
- Roll,
24
- TemporalLiteral,
25
- TimeUnit,
26
- )
27
-
28
- if sys.version_info >= (3, 13):
29
- from warnings import deprecated
30
- else:
31
- from typing_extensions import deprecated # noqa: TC004
32
-
33
-
34
- @expr_dispatch
35
- class DateTimeNameSpace:
36
- """Series.dt namespace."""
37
-
38
- _accessor = "dt"
39
-
40
- def __init__(self, series: Series) -> None:
41
- self._s: PySeries = series._s
42
-
43
- def __getitem__(self, item: int) -> dt.date | dt.datetime | dt.timedelta:
44
- s = wrap_s(self._s)
45
- return s[item]
46
-
47
- @unstable()
48
- @deprecate_nonkeyword_arguments(allowed_args=["self", "n"], version="1.27.0")
49
- def add_business_days(
50
- self,
51
- n: int | IntoExpr,
52
- week_mask: Iterable[bool] = (True, True, True, True, True, False, False),
53
- holidays: Iterable[dt.date] = (),
54
- roll: Roll = "raise",
55
- ) -> Series:
56
- """
57
- Offset by `n` business days.
58
-
59
- .. warning::
60
- This functionality is considered **unstable**. It may be changed
61
- at any point without it being considered a breaking change.
62
-
63
- .. versionchanged:: 1.27.0
64
- Parameters after `n` should now be passed as keyword arguments.
65
-
66
- Parameters
67
- ----------
68
- n
69
- Number of business days to offset by. Can be a single number of an
70
- expression.
71
- week_mask
72
- Which days of the week to count. The default is Monday to Friday.
73
- If you wanted to count only Monday to Thursday, you would pass
74
- `(True, True, True, True, False, False, False)`.
75
- holidays
76
- Holidays to exclude from the count. The Python package
77
- `python-holidays <https://github.com/vacanza/python-holidays>`_
78
- may come in handy here. You can install it with ``pip install holidays``,
79
- and then, to get all Dutch holidays for years 2020-2024:
80
-
81
- .. code-block:: python
82
-
83
- import holidays
84
-
85
- my_holidays = holidays.country_holidays("NL", years=range(2020, 2025))
86
-
87
- and pass `holidays=my_holidays` when you call `add_business_days`.
88
- roll
89
- What to do when the start date lands on a non-business day. Options are:
90
-
91
- - `'raise'`: raise an error
92
- - `'forward'`: move to the next business day
93
- - `'backward'`: move to the previous business day
94
-
95
- Returns
96
- -------
97
- Series
98
- Data type is preserved.
99
-
100
- Examples
101
- --------
102
- >>> from datetime import date
103
- >>> s = pl.Series("start", [date(2020, 1, 1), date(2020, 1, 2)])
104
- >>> s.dt.add_business_days(5)
105
- shape: (2,)
106
- Series: 'start' [date]
107
- [
108
- 2020-01-08
109
- 2020-01-09
110
- ]
111
-
112
- You can pass a custom weekend - for example, if you only take Sunday off:
113
-
114
- >>> week_mask = (True, True, True, True, True, True, False)
115
- >>> s.dt.add_business_days(5, week_mask=week_mask)
116
- shape: (2,)
117
- Series: 'start' [date]
118
- [
119
- 2020-01-07
120
- 2020-01-08
121
- ]
122
-
123
- You can also pass a list of holidays:
124
-
125
- >>> from datetime import date
126
- >>> holidays = [date(2020, 1, 3), date(2020, 1, 6)]
127
- >>> s.dt.add_business_days(5, holidays=holidays)
128
- shape: (2,)
129
- Series: 'start' [date]
130
- [
131
- 2020-01-10
132
- 2020-01-13
133
- ]
134
-
135
- Roll all dates forwards to the next business day:
136
-
137
- >>> s = pl.Series("start", [date(2020, 1, 5), date(2020, 1, 6)])
138
- >>> s.dt.add_business_days(0, roll="forward")
139
- shape: (2,)
140
- Series: 'start' [date]
141
- [
142
- 2020-01-06
143
- 2020-01-06
144
- ]
145
- """
146
-
147
- def min(self) -> dt.date | dt.datetime | dt.timedelta | None:
148
- """
149
- Return minimum as Python datetime.
150
-
151
- Examples
152
- --------
153
- >>> from datetime import date
154
- >>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2), date(2001, 1, 3)])
155
- >>> s.dt.min()
156
- datetime.date(2001, 1, 1)
157
- """
158
- return wrap_s(self._s).min() # type: ignore[return-value]
159
-
160
- def max(self) -> dt.date | dt.datetime | dt.timedelta | None:
161
- """
162
- Return maximum as Python datetime.
163
-
164
- Examples
165
- --------
166
- >>> from datetime import date
167
- >>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2), date(2001, 1, 3)])
168
- >>> s.dt.max()
169
- datetime.date(2001, 1, 3)
170
- """
171
- return wrap_s(self._s).max() # type: ignore[return-value]
172
-
173
- @deprecated("`Series.dt.median` is deprecated; use `Series.median` instead.")
174
- def median(self) -> TemporalLiteral | None:
175
- """
176
- Return median as python DateTime.
177
-
178
- .. deprecated:: 1.0.0
179
- Use the `Series.median` method instead.
180
-
181
- Examples
182
- --------
183
- >>> from datetime import date, datetime
184
- >>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2)])
185
- >>> s.dt.median() # doctest: +SKIP
186
- datetime.datetime(2001, 1, 1, 12, 0)
187
- >>> date = pl.datetime_range(
188
- ... datetime(2001, 1, 1), datetime(2001, 1, 3), "1d", eager=True
189
- ... ).alias("datetime")
190
- >>> date
191
- shape: (3,)
192
- Series: 'datetime' [datetime[μs]]
193
- [
194
- 2001-01-01 00:00:00
195
- 2001-01-02 00:00:00
196
- 2001-01-03 00:00:00
197
- ]
198
- >>> date.dt.median() # doctest: +SKIP
199
- datetime.datetime(2001, 1, 2, 0, 0)
200
- """
201
- return self._s.median()
202
-
203
- @deprecated("`Series.dt.mean` is deprecated; use `Series.mean` instead.")
204
- def mean(self) -> TemporalLiteral | None:
205
- """
206
- Return mean as python DateTime.
207
-
208
- .. deprecated:: 1.0.0
209
- Use the `Series.mean` method instead.
210
-
211
- Examples
212
- --------
213
- >>> from datetime import date, datetime
214
- >>> s = pl.Series([date(2001, 1, 1), date(2001, 1, 2)])
215
- >>> s.dt.mean() # doctest: +SKIP
216
- datetime.datetime(2001, 1, 1, 12, 0)
217
- >>> s = pl.Series(
218
- ... [datetime(2001, 1, 1), datetime(2001, 1, 2), datetime(2001, 1, 3)]
219
- ... )
220
- >>> s.dt.mean() # doctest: +SKIP
221
- datetime.datetime(2001, 1, 2, 0, 0)
222
- """
223
- return self._s.mean()
224
-
225
- def to_string(self, format: str | None = None) -> Series:
226
- """
227
- Convert a Date/Time/Datetime column into a String column with the given format.
228
-
229
- .. versionchanged:: 1.15.0
230
- Added support for the use of "iso:strict" as a format string.
231
- .. versionchanged:: 1.14.0
232
- Added support for the `Duration` dtype, and use of "iso" as a format string.
233
-
234
- Parameters
235
- ----------
236
- format
237
- * Format to use, refer to the `chrono strftime documentation
238
- <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
239
- for specification. Example: `"%y-%m-%d"`.
240
-
241
- * If no format is provided, the appropriate ISO format for the underlying
242
- data type is used. This can be made explicit by passing `"iso"` or
243
- `"iso:strict"` as the format string (see notes below for details).
244
-
245
- Notes
246
- -----
247
- * Similar to `cast(pl.String)`, but this method allows you to customize
248
- the formatting of the resulting string; if no format is provided, the
249
- appropriate ISO format for the underlying data type is used.
250
-
251
- * Datetime dtype expressions distinguish between "iso" and "iso:strict"
252
- format strings. The difference is in the inclusion of a "T" separator
253
- between the date and time components ("iso" results in ISO compliant
254
- date and time components, separated with a space; "iso:strict" returns
255
- the same components separated with a "T"). All other temporal types
256
- return the same value for both format strings.
257
-
258
- * Duration dtype expressions cannot be formatted with `strftime`. Instead,
259
- only "iso" and "polars" are supported as format strings. The "iso" format
260
- string results in ISO8601 duration string output, and "polars" results
261
- in the same form seen in the frame `repr`.
262
-
263
- Examples
264
- --------
265
- >>> from datetime import datetime
266
- >>> s = pl.Series(
267
- ... "dtm",
268
- ... [
269
- ... datetime(1999, 12, 31, 6, 12, 30, 800),
270
- ... datetime(2020, 7, 5, 10, 20, 45, 12345),
271
- ... datetime(2077, 10, 20, 18, 25, 10, 999999),
272
- ... ],
273
- ... )
274
-
275
- Default for temporal dtypes (if not specifying a format string) is ISO8601:
276
-
277
- >>> s.dt.to_string() # or s.dt.to_string("iso")
278
- shape: (3,)
279
- Series: 'dtm' [str]
280
- [
281
- "1999-12-31 06:12:30.000800"
282
- "2020-07-05 10:20:45.012345"
283
- "2077-10-20 18:25:10.999999"
284
- ]
285
-
286
- For `Datetime` specifically you can choose between "iso" (where the date and
287
- time components are ISO, separated by a space) and "iso:strict" (where these
288
- components are separated by a "T"):
289
-
290
- >>> s.dt.to_string("iso:strict")
291
- shape: (3,)
292
- Series: 'dtm' [str]
293
- [
294
- "1999-12-31T06:12:30.000800"
295
- "2020-07-05T10:20:45.012345"
296
- "2077-10-20T18:25:10.999999"
297
- ]
298
-
299
- The output can be customized by using a strftime-compatible format string:
300
-
301
- >>> s.dt.to_string("%d/%m/%y")
302
- shape: (3,)
303
- Series: 'dtm' [str]
304
- [
305
- "31/12/99"
306
- "05/07/20"
307
- "20/10/77"
308
- ]
309
-
310
- If you're interested in using day or month names, you can use
311
- the `'%A'` and/or `'%B'` format strings:
312
-
313
- >>> s.dt.to_string("%A")
314
- shape: (3,)
315
- Series: 'dtm' [str]
316
- [
317
- "Friday"
318
- "Sunday"
319
- "Wednesday"
320
- ]
321
-
322
- >>> s.dt.to_string("%B")
323
- shape: (3,)
324
- Series: 'dtm' [str]
325
- [
326
- "December"
327
- "July"
328
- "October"
329
- ]
330
- """
331
-
332
- def strftime(self, format: str) -> Series:
333
- """
334
- Convert a Date/Time/Datetime column into a String column with the given format.
335
-
336
- Similar to `cast(pl.String)`, but this method allows you to customize the
337
- formatting of the resulting string.
338
-
339
- Alias for :func:`to_string`.
340
-
341
- Parameters
342
- ----------
343
- format
344
- Format to use, refer to the `chrono strftime documentation
345
- <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
346
- for specification. Example: `"%y-%m-%d"`.
347
-
348
- See Also
349
- --------
350
- to_string : The identical Series method for which `strftime` is an alias.
351
-
352
- Examples
353
- --------
354
- >>> from datetime import datetime
355
- >>> s = pl.Series(
356
- ... "datetime",
357
- ... [datetime(2020, 3, 1), datetime(2020, 4, 1), datetime(2020, 5, 1)],
358
- ... )
359
- >>> s.dt.strftime("%Y/%m/%d")
360
- shape: (3,)
361
- Series: 'datetime' [str]
362
- [
363
- "2020/03/01"
364
- "2020/04/01"
365
- "2020/05/01"
366
- ]
367
-
368
- If you're interested in the day name / month name, you can use
369
- `'%A'` / `'%B'`:
370
-
371
- >>> s.dt.strftime("%A")
372
- shape: (3,)
373
- Series: 'datetime' [str]
374
- [
375
- "Sunday"
376
- "Wednesday"
377
- "Friday"
378
- ]
379
-
380
- >>> s.dt.strftime("%B")
381
- shape: (3,)
382
- Series: 'datetime' [str]
383
- [
384
- "March"
385
- "April"
386
- "May"
387
- ]
388
- """
389
- return self.to_string(format)
390
-
391
- def millennium(self) -> Series:
392
- """
393
- Extract the millennium from underlying representation.
394
-
395
- Applies to Date and Datetime columns.
396
-
397
- Returns the millennium number in the calendar date.
398
-
399
- Returns
400
- -------
401
- Series
402
- Series of data type :class:`Int32`.
403
-
404
- Examples
405
- --------
406
- >>> from datetime import date
407
- >>> s = pl.Series(
408
- ... "dt",
409
- ... [
410
- ... date(999, 12, 31),
411
- ... date(1897, 5, 7),
412
- ... date(2000, 1, 1),
413
- ... date(2001, 7, 5),
414
- ... date(3002, 10, 20),
415
- ... ],
416
- ... )
417
- >>> s.dt.millennium()
418
- shape: (5,)
419
- Series: 'dt' [i32]
420
- [
421
- 1
422
- 2
423
- 2
424
- 3
425
- 4
426
- ]
427
- """
428
-
429
- def century(self) -> Series:
430
- """
431
- Extract the century from underlying representation.
432
-
433
- Applies to Date and Datetime columns.
434
-
435
- Returns the century number in the calendar date.
436
-
437
- Returns
438
- -------
439
- Series
440
- Series of data type :class:`Int32`.
441
-
442
- Examples
443
- --------
444
- >>> from datetime import date
445
- >>> s = pl.Series(
446
- ... "dt",
447
- ... [
448
- ... date(999, 12, 31),
449
- ... date(1897, 5, 7),
450
- ... date(2000, 1, 1),
451
- ... date(2001, 7, 5),
452
- ... date(3002, 10, 20),
453
- ... ],
454
- ... )
455
- >>> s.dt.century()
456
- shape: (5,)
457
- Series: 'dt' [i32]
458
- [
459
- 10
460
- 19
461
- 20
462
- 21
463
- 31
464
- ]
465
- """
466
-
467
- def year(self) -> Series:
468
- """
469
- Extract the year from the underlying date representation.
470
-
471
- Applies to Date and Datetime columns.
472
-
473
- Returns the year number in the calendar date.
474
-
475
- Returns
476
- -------
477
- Series
478
- Series of data type :class:`Int32`.
479
-
480
- Examples
481
- --------
482
- >>> from datetime import date
483
- >>> s = pl.Series("date", [date(2001, 1, 1), date(2002, 1, 1)])
484
- >>> s.dt.year()
485
- shape: (2,)
486
- Series: 'date' [i32]
487
- [
488
- 2001
489
- 2002
490
- ]
491
- """
492
-
493
- @unstable()
494
- def is_business_day(
495
- self,
496
- *,
497
- week_mask: Iterable[bool] = (True, True, True, True, True, False, False),
498
- holidays: Iterable[dt.date] = (),
499
- ) -> Series:
500
- """
501
- Determine whether each day lands on a business day.
502
-
503
- .. warning::
504
- This functionality is considered **unstable**. It may be changed
505
- at any point without it being considered a breaking change.
506
-
507
- Parameters
508
- ----------
509
- week_mask
510
- Which days of the week to count. The default is Monday to Friday.
511
- If you wanted to count only Monday to Thursday, you would pass
512
- `(True, True, True, True, False, False, False)`.
513
- holidays
514
- Holidays to exclude from the count. The Python package
515
- `python-holidays <https://github.com/vacanza/python-holidays>`_
516
- may come in handy here. You can install it with ``pip install holidays``,
517
- and then, to get all Dutch holidays for years 2020-2024:
518
-
519
- .. code-block:: python
520
-
521
- import holidays
522
-
523
- my_holidays = holidays.country_holidays("NL", years=range(2020, 2025))
524
-
525
- and pass `holidays=my_holidays` when you call `is_business_day`.
526
-
527
- Returns
528
- -------
529
- Series
530
- Series of data type :class:`Boolean`.
531
-
532
- Examples
533
- --------
534
- >>> from datetime import date
535
- >>> s = pl.Series([date(2020, 1, 3), date(2020, 1, 5)])
536
- >>> s.dt.is_business_day()
537
- shape: (2,)
538
- Series: '' [bool]
539
- [
540
- true
541
- false
542
- ]
543
-
544
- You can pass a custom weekend - for example, if you only take Sunday off:
545
-
546
- >>> week_mask = (True, True, True, True, True, True, False)
547
- >>> s.dt.is_business_day(week_mask=week_mask)
548
- shape: (2,)
549
- Series: '' [bool]
550
- [
551
- true
552
- false
553
- ]
554
-
555
- You can also pass a list of holidays:
556
-
557
- >>> from datetime import date
558
- >>> holidays = [date(2020, 1, 3), date(2020, 1, 6)]
559
- >>> s.dt.is_business_day(holidays=holidays)
560
- shape: (2,)
561
- Series: '' [bool]
562
- [
563
- false
564
- false
565
- ]
566
- """
567
-
568
- def is_leap_year(self) -> Series:
569
- """
570
- Determine whether the year of the underlying date representation is a leap year.
571
-
572
- Applies to Date and Datetime columns.
573
-
574
- Returns
575
- -------
576
- Series
577
- Series of data type :class:`Boolean`.
578
-
579
- Examples
580
- --------
581
- >>> from datetime import date
582
- >>> s = pl.Series(
583
- ... "date", [date(2000, 1, 1), date(2001, 1, 1), date(2002, 1, 1)]
584
- ... )
585
- >>> s.dt.is_leap_year()
586
- shape: (3,)
587
- Series: 'date' [bool]
588
- [
589
- true
590
- false
591
- false
592
- ]
593
- """
594
-
595
- def iso_year(self) -> Series:
596
- """
597
- Extract ISO year from underlying Date representation.
598
-
599
- Applies to Date and Datetime columns.
600
-
601
- Returns the year number according to the ISO standard.
602
- This may not correspond with the calendar year.
603
-
604
- Returns
605
- -------
606
- Series
607
- Series of data type :class:`Int32`.
608
-
609
- Examples
610
- --------
611
- >>> from datetime import datetime
612
- >>> dt = datetime(2022, 1, 1, 7, 8, 40)
613
- >>> pl.Series([dt]).dt.iso_year()
614
- shape: (1,)
615
- Series: '' [i32]
616
- [
617
- 2021
618
- ]
619
- """
620
-
621
- def quarter(self) -> Series:
622
- """
623
- Extract quarter from underlying Date representation.
624
-
625
- Applies to Date and Datetime columns.
626
-
627
- Returns the quarter ranging from 1 to 4.
628
-
629
- Returns
630
- -------
631
- Series
632
- Series of data type :class:`Int8`.
633
-
634
- Examples
635
- --------
636
- >>> from datetime import date
637
- >>> date = pl.date_range(
638
- ... date(2001, 1, 1), date(2001, 4, 1), interval="1mo", eager=True
639
- ... ).alias("date")
640
- >>> date.dt.quarter()
641
- shape: (4,)
642
- Series: 'date' [i8]
643
- [
644
- 1
645
- 1
646
- 1
647
- 2
648
- ]
649
- """
650
-
651
- def month(self) -> Series:
652
- """
653
- Extract the month from the underlying date representation.
654
-
655
- Applies to Date and Datetime columns.
656
-
657
- Returns the month number starting from 1.
658
- The return value ranges from 1 to 12.
659
-
660
- Returns
661
- -------
662
- Series
663
- Series of data type :class:`Int8`.
664
-
665
- Examples
666
- --------
667
- >>> from datetime import date
668
- >>> date = pl.date_range(
669
- ... date(2001, 1, 1), date(2001, 4, 1), interval="1mo", eager=True
670
- ... ).alias("date")
671
- >>> date.dt.month()
672
- shape: (4,)
673
- Series: 'date' [i8]
674
- [
675
- 1
676
- 2
677
- 3
678
- 4
679
- ]
680
- """
681
-
682
- def days_in_month(self) -> Series:
683
- """
684
- Extract the number of days in the month from the underlying date representation.
685
-
686
- Applies to Date and Datetime columns.
687
-
688
- Returns the number of days in the month.
689
- The return value ranges from 28 to 31.
690
-
691
- Returns
692
- -------
693
- Series
694
- Series of data type :class:`Int8`.
695
-
696
- See Also
697
- --------
698
- month
699
- is_leap_year
700
-
701
- Examples
702
- --------
703
- >>> from datetime import date
704
- >>> s = pl.Series(
705
- ... "date", [date(2001, 1, 1), date(2001, 2, 1), date(2000, 2, 1)]
706
- ... )
707
- >>> s.dt.days_in_month()
708
- shape: (3,)
709
- Series: 'date' [i8]
710
- [
711
- 31
712
- 28
713
- 29
714
- ]
715
- """
716
-
717
- def week(self) -> Series:
718
- """
719
- Extract the week from the underlying date representation.
720
-
721
- Applies to Date and Datetime columns.
722
-
723
- Returns the ISO week number starting from 1.
724
- The return value ranges from 1 to 53. (The last week of year differs by years.)
725
-
726
- Returns
727
- -------
728
- Series
729
- Series of data type :class:`Int8`.
730
-
731
- Examples
732
- --------
733
- >>> from datetime import date
734
- >>> date = pl.date_range(
735
- ... date(2001, 1, 1), date(2001, 4, 1), interval="1mo", eager=True
736
- ... ).alias("date")
737
- >>> date.dt.week()
738
- shape: (4,)
739
- Series: 'date' [i8]
740
- [
741
- 1
742
- 5
743
- 9
744
- 13
745
- ]
746
- """
747
-
748
- def weekday(self) -> Series:
749
- """
750
- Extract the week day from the underlying date representation.
751
-
752
- Applies to Date and Datetime columns.
753
-
754
- Returns the ISO weekday number where monday = 1 and sunday = 7
755
-
756
- Returns
757
- -------
758
- Series
759
- Series of data type :class:`Int8`.
760
-
761
- Examples
762
- --------
763
- >>> from datetime import date
764
- >>> s = pl.date_range(date(2001, 1, 1), date(2001, 1, 7), eager=True).alias(
765
- ... "date"
766
- ... )
767
- >>> s.dt.weekday()
768
- shape: (7,)
769
- Series: 'date' [i8]
770
- [
771
- 1
772
- 2
773
- 3
774
- 4
775
- 5
776
- 6
777
- 7
778
- ]
779
- """
780
-
781
- def day(self) -> Series:
782
- """
783
- Extract the day from the underlying date representation.
784
-
785
- Applies to Date and Datetime columns.
786
-
787
- Returns the day of month starting from 1.
788
- The return value ranges from 1 to 31. (The last day of month differs by months.)
789
-
790
- Returns
791
- -------
792
- Series
793
- Series of data type :class:`Int8`.
794
-
795
- Examples
796
- --------
797
- >>> from datetime import date
798
- >>> s = pl.date_range(
799
- ... date(2001, 1, 1), date(2001, 1, 9), interval="2d", eager=True
800
- ... ).alias("date")
801
- >>> s.dt.day()
802
- shape: (5,)
803
- Series: 'date' [i8]
804
- [
805
- 1
806
- 3
807
- 5
808
- 7
809
- 9
810
- ]
811
- """
812
-
813
- def ordinal_day(self) -> Series:
814
- """
815
- Extract ordinal day from underlying date representation.
816
-
817
- Applies to Date and Datetime columns.
818
-
819
- Returns the day of year starting from 1.
820
- The return value ranges from 1 to 366. (The last day of year differs by years.)
821
-
822
- Returns
823
- -------
824
- Series
825
- Series of data type :class:`Int16`.
826
-
827
- Examples
828
- --------
829
- >>> from datetime import date
830
- >>> s = pl.date_range(
831
- ... date(2001, 1, 1), date(2001, 3, 1), interval="1mo", eager=True
832
- ... ).alias("date")
833
- >>> s.dt.ordinal_day()
834
- shape: (3,)
835
- Series: 'date' [i16]
836
- [
837
- 1
838
- 32
839
- 60
840
- ]
841
- """
842
-
843
- def time(self) -> Series:
844
- """
845
- Extract (local) time.
846
-
847
- Applies to Date/Datetime/Time columns.
848
-
849
- Returns
850
- -------
851
- Series
852
- Series of data type :class:`Time`.
853
-
854
- Examples
855
- --------
856
- >>> from datetime import datetime
857
- >>> ser = pl.Series([datetime(2021, 1, 2, 5)]).dt.replace_time_zone(
858
- ... "Asia/Kathmandu"
859
- ... )
860
- >>> ser
861
- shape: (1,)
862
- Series: '' [datetime[μs, Asia/Kathmandu]]
863
- [
864
- 2021-01-02 05:00:00 +0545
865
- ]
866
- >>> ser.dt.time()
867
- shape: (1,)
868
- Series: '' [time]
869
- [
870
- 05:00:00
871
- ]
872
- """
873
-
874
- def date(self) -> Series:
875
- """
876
- Extract (local) date.
877
-
878
- Applies to Date/Datetime columns.
879
-
880
- Returns
881
- -------
882
- Series
883
- Series of data type :class:`Date`.
884
-
885
- Examples
886
- --------
887
- >>> from datetime import datetime
888
- >>> ser = pl.Series([datetime(2021, 1, 2, 5)]).dt.replace_time_zone(
889
- ... "Asia/Kathmandu"
890
- ... )
891
- >>> ser
892
- shape: (1,)
893
- Series: '' [datetime[μs, Asia/Kathmandu]]
894
- [
895
- 2021-01-02 05:00:00 +0545
896
- ]
897
- >>> ser.dt.date()
898
- shape: (1,)
899
- Series: '' [date]
900
- [
901
- 2021-01-02
902
- ]
903
- """
904
-
905
- @deprecated(
906
- "`Series.dt.datetime` is deprecated; "
907
- "use `Series.dt.replace_time_zone(None)` instead."
908
- )
909
- def datetime(self) -> Series:
910
- """
911
- Extract (local) datetime.
912
-
913
- .. deprecated:: 0.20.4
914
- Use `dt.replace_time_zone(None)` instead.
915
-
916
- Applies to Datetime columns.
917
-
918
- Returns
919
- -------
920
- Series
921
- Series of data type :class:`Datetime`.
922
-
923
- Examples
924
- --------
925
- >>> from datetime import datetime
926
- >>> ser = pl.Series([datetime(2021, 1, 2, 5)]).dt.replace_time_zone(
927
- ... "Asia/Kathmandu"
928
- ... )
929
- >>> ser
930
- shape: (1,)
931
- Series: '' [datetime[μs, Asia/Kathmandu]]
932
- [
933
- 2021-01-02 05:00:00 +0545
934
- ]
935
- >>> ser.dt.datetime() # doctest: +SKIP
936
- shape: (1,)
937
- Series: '' [datetime[μs]]
938
- [
939
- 2021-01-02 05:00:00
940
- ]
941
- """
942
-
943
- def hour(self) -> Series:
944
- """
945
- Extract the hour from the underlying DateTime representation.
946
-
947
- Applies to Datetime columns.
948
-
949
- Returns the hour number from 0 to 23.
950
-
951
- Returns
952
- -------
953
- Series
954
- Series of data type :class:`Int8`.
955
-
956
- Examples
957
- --------
958
- >>> from datetime import datetime
959
- >>> start = datetime(2001, 1, 1)
960
- >>> stop = datetime(2001, 1, 1, 3)
961
- >>> date = pl.datetime_range(start, stop, interval="1h", eager=True).alias(
962
- ... "datetime"
963
- ... )
964
- >>> date
965
- shape: (4,)
966
- Series: 'datetime' [datetime[μs]]
967
- [
968
- 2001-01-01 00:00:00
969
- 2001-01-01 01:00:00
970
- 2001-01-01 02:00:00
971
- 2001-01-01 03:00:00
972
- ]
973
- >>> date.dt.hour()
974
- shape: (4,)
975
- Series: 'datetime' [i8]
976
- [
977
- 0
978
- 1
979
- 2
980
- 3
981
- ]
982
- """
983
-
984
- def minute(self) -> Series:
985
- """
986
- Extract the minutes from the underlying DateTime representation.
987
-
988
- Applies to Datetime columns.
989
-
990
- Returns the minute number from 0 to 59.
991
-
992
- Returns
993
- -------
994
- Series
995
- Series of data type :class:`Int8`.
996
-
997
- Examples
998
- --------
999
- >>> from datetime import datetime
1000
- >>> start = datetime(2001, 1, 1)
1001
- >>> stop = datetime(2001, 1, 1, 0, 4, 0)
1002
- >>> date = pl.datetime_range(start, stop, interval="2m", eager=True).alias(
1003
- ... "datetime"
1004
- ... )
1005
- >>> date
1006
- shape: (3,)
1007
- Series: 'datetime' [datetime[μs]]
1008
- [
1009
- 2001-01-01 00:00:00
1010
- 2001-01-01 00:02:00
1011
- 2001-01-01 00:04:00
1012
- ]
1013
- >>> date.dt.minute()
1014
- shape: (3,)
1015
- Series: 'datetime' [i8]
1016
- [
1017
- 0
1018
- 2
1019
- 4
1020
- ]
1021
- """
1022
-
1023
- def second(self, *, fractional: bool = False) -> Series:
1024
- """
1025
- Extract seconds from underlying DateTime representation.
1026
-
1027
- Applies to Datetime columns.
1028
-
1029
- Returns the integer second number from 0 to 59, or a floating
1030
- point number from 0 < 60 if `fractional=True` that includes
1031
- any milli/micro/nanosecond component.
1032
-
1033
- Parameters
1034
- ----------
1035
- fractional
1036
- Whether to include the fractional component of the second.
1037
-
1038
- Returns
1039
- -------
1040
- Series
1041
- Series of data type :class:`Int8` or :class:`Float64`.
1042
-
1043
- Examples
1044
- --------
1045
- >>> from datetime import datetime
1046
- >>> s = pl.Series(
1047
- ... "datetime",
1048
- ... [
1049
- ... datetime(2000, 1, 1, 0, 0, 0, 456789),
1050
- ... datetime(2000, 1, 1, 0, 0, 3, 111110),
1051
- ... datetime(2000, 1, 1, 0, 0, 5, 765431),
1052
- ... ],
1053
- ... )
1054
- >>> s.dt.second()
1055
- shape: (3,)
1056
- Series: 'datetime' [i8]
1057
- [
1058
- 0
1059
- 3
1060
- 5
1061
- ]
1062
- >>> s.dt.second(fractional=True)
1063
- shape: (3,)
1064
- Series: 'datetime' [f64]
1065
- [
1066
- 0.456789
1067
- 3.11111
1068
- 5.765431
1069
- ]
1070
- """
1071
-
1072
- def millisecond(self) -> Series:
1073
- """
1074
- Extract the milliseconds from the underlying DateTime representation.
1075
-
1076
- Applies to Datetime columns.
1077
-
1078
- Returns
1079
- -------
1080
- Series
1081
- Series of data type :class:`Int32`.
1082
-
1083
- Examples
1084
- --------
1085
- >>> from datetime import datetime
1086
- >>> start = datetime(2001, 1, 1)
1087
- >>> stop = datetime(2001, 1, 1, 0, 0, 4)
1088
- >>> s = pl.datetime_range(start, stop, interval="500ms", eager=True).alias(
1089
- ... "datetime"
1090
- ... )
1091
- >>> s.dt.millisecond()
1092
- shape: (9,)
1093
- Series: 'datetime' [i32]
1094
- [
1095
- 0
1096
- 500
1097
- 0
1098
- 500
1099
- 0
1100
- 500
1101
- 0
1102
- 500
1103
- 0
1104
- ]
1105
- """
1106
-
1107
- def microsecond(self) -> Series:
1108
- """
1109
- Extract the microseconds from the underlying DateTime representation.
1110
-
1111
- Applies to Datetime columns.
1112
-
1113
- Returns
1114
- -------
1115
- Series
1116
- Series of data type :class:`Int32`.
1117
-
1118
- Examples
1119
- --------
1120
- >>> from datetime import datetime
1121
- >>> start = datetime(2001, 1, 1)
1122
- >>> stop = datetime(2001, 1, 1, 0, 0, 4)
1123
- >>> date = pl.datetime_range(start, stop, interval="500ms", eager=True).alias(
1124
- ... "datetime"
1125
- ... )
1126
- >>> date
1127
- shape: (9,)
1128
- Series: 'datetime' [datetime[μs]]
1129
- [
1130
- 2001-01-01 00:00:00
1131
- 2001-01-01 00:00:00.500
1132
- 2001-01-01 00:00:01
1133
- 2001-01-01 00:00:01.500
1134
- 2001-01-01 00:00:02
1135
- 2001-01-01 00:00:02.500
1136
- 2001-01-01 00:00:03
1137
- 2001-01-01 00:00:03.500
1138
- 2001-01-01 00:00:04
1139
- ]
1140
- >>> date.dt.microsecond()
1141
- shape: (9,)
1142
- Series: 'datetime' [i32]
1143
- [
1144
- 0
1145
- 500000
1146
- 0
1147
- 500000
1148
- 0
1149
- 500000
1150
- 0
1151
- 500000
1152
- 0
1153
- ]
1154
- """
1155
-
1156
- def nanosecond(self) -> Series:
1157
- """
1158
- Extract the nanoseconds from the underlying DateTime representation.
1159
-
1160
- Applies to Datetime columns.
1161
-
1162
- Returns
1163
- -------
1164
- Series
1165
- Series of data type :class:`Int32`.
1166
-
1167
- Examples
1168
- --------
1169
- >>> from datetime import datetime
1170
- >>> start = datetime(2001, 1, 1)
1171
- >>> stop = datetime(2001, 1, 1, 0, 0, 4)
1172
- >>> date = pl.datetime_range(start, stop, interval="500ms", eager=True).alias(
1173
- ... "datetime"
1174
- ... )
1175
- >>> date
1176
- shape: (9,)
1177
- Series: 'datetime' [datetime[μs]]
1178
- [
1179
- 2001-01-01 00:00:00
1180
- 2001-01-01 00:00:00.500
1181
- 2001-01-01 00:00:01
1182
- 2001-01-01 00:00:01.500
1183
- 2001-01-01 00:00:02
1184
- 2001-01-01 00:00:02.500
1185
- 2001-01-01 00:00:03
1186
- 2001-01-01 00:00:03.500
1187
- 2001-01-01 00:00:04
1188
- ]
1189
- >>> date.dt.nanosecond()
1190
- shape: (9,)
1191
- Series: 'datetime' [i32]
1192
- [
1193
- 0
1194
- 500000000
1195
- 0
1196
- 500000000
1197
- 0
1198
- 500000000
1199
- 0
1200
- 500000000
1201
- 0
1202
- ]
1203
- """
1204
-
1205
- def timestamp(self, time_unit: TimeUnit = "us") -> Series:
1206
- """
1207
- Return a timestamp in the given time unit.
1208
-
1209
- Parameters
1210
- ----------
1211
- time_unit : {'us', 'ns', 'ms'}
1212
- Time unit.
1213
-
1214
- Examples
1215
- --------
1216
- >>> from datetime import datetime
1217
- >>> start = datetime(2001, 1, 1)
1218
- >>> stop = datetime(2001, 1, 3)
1219
- >>> date = pl.datetime_range(start, stop, interval="1d", eager=True).alias(
1220
- ... "datetime"
1221
- ... )
1222
- >>> date
1223
- shape: (3,)
1224
- Series: 'datetime' [datetime[μs]]
1225
- [
1226
- 2001-01-01 00:00:00
1227
- 2001-01-02 00:00:00
1228
- 2001-01-03 00:00:00
1229
- ]
1230
- >>> date.dt.timestamp().alias("timestamp_us")
1231
- shape: (3,)
1232
- Series: 'timestamp_us' [i64]
1233
- [
1234
- 978307200000000
1235
- 978393600000000
1236
- 978480000000000
1237
- ]
1238
- >>> date.dt.timestamp("ns").alias("timestamp_ns")
1239
- shape: (3,)
1240
- Series: 'timestamp_ns' [i64]
1241
- [
1242
- 978307200000000000
1243
- 978393600000000000
1244
- 978480000000000000
1245
- ]
1246
- """
1247
-
1248
- def epoch(self, time_unit: EpochTimeUnit = "us") -> Series:
1249
- """
1250
- Get the time passed since the Unix EPOCH in the give time unit.
1251
-
1252
- Parameters
1253
- ----------
1254
- time_unit : {'us', 'ns', 'ms', 's', 'd'}
1255
- Unit of time.
1256
-
1257
- Examples
1258
- --------
1259
- >>> from datetime import datetime
1260
- >>> start = datetime(2001, 1, 1)
1261
- >>> stop = datetime(2001, 1, 3)
1262
- >>> date = pl.datetime_range(start, stop, interval="1d", eager=True).alias(
1263
- ... "datetime"
1264
- ... )
1265
- >>> date
1266
- shape: (3,)
1267
- Series: 'datetime' [datetime[μs]]
1268
- [
1269
- 2001-01-01 00:00:00
1270
- 2001-01-02 00:00:00
1271
- 2001-01-03 00:00:00
1272
- ]
1273
- >>> date.dt.epoch().alias("epoch_ns")
1274
- shape: (3,)
1275
- Series: 'epoch_ns' [i64]
1276
- [
1277
- 978307200000000
1278
- 978393600000000
1279
- 978480000000000
1280
- ]
1281
- >>> date.dt.epoch(time_unit="s").alias("epoch_s")
1282
- shape: (3,)
1283
- Series: 'epoch_s' [i64]
1284
- [
1285
- 978307200
1286
- 978393600
1287
- 978480000
1288
- ]
1289
- """
1290
-
1291
- def with_time_unit(self, time_unit: TimeUnit) -> Series:
1292
- """
1293
- Set time unit a Series of dtype Datetime or Duration.
1294
-
1295
- .. deprecated:: 0.20.5
1296
- First cast to `Int64` and then cast to the desired data type.
1297
-
1298
- This does not modify underlying data, and should be used to fix an incorrect
1299
- time unit.
1300
-
1301
- Parameters
1302
- ----------
1303
- time_unit : {'ns', 'us', 'ms'}
1304
- Unit of time for the `Datetime` or `Duration` Series.
1305
-
1306
- Examples
1307
- --------
1308
- >>> from datetime import datetime
1309
- >>> s = pl.Series(
1310
- ... "datetime",
1311
- ... [datetime(2001, 1, 1), datetime(2001, 1, 2), datetime(2001, 1, 3)],
1312
- ... dtype=pl.Datetime(time_unit="ns"),
1313
- ... )
1314
- >>> s.dt.with_time_unit("us") # doctest: +SKIP
1315
- shape: (3,)
1316
- Series: 'datetime' [datetime[μs]]
1317
- [
1318
- +32971-04-28 00:00:00
1319
- +32974-01-22 00:00:00
1320
- +32976-10-18 00:00:00
1321
- ]
1322
- """
1323
-
1324
- def cast_time_unit(self, time_unit: TimeUnit) -> Series:
1325
- """
1326
- Cast the underlying data to another time unit. This may lose precision.
1327
-
1328
- Parameters
1329
- ----------
1330
- time_unit : {'ns', 'us', 'ms'}
1331
- Unit of time for the `Datetime` Series.
1332
-
1333
- Examples
1334
- --------
1335
- >>> from datetime import datetime
1336
- >>> start = datetime(2001, 1, 1)
1337
- >>> stop = datetime(2001, 1, 3)
1338
- >>> date = pl.datetime_range(start, stop, "1d", eager=True).alias("datetime")
1339
- >>> date
1340
- shape: (3,)
1341
- Series: 'datetime' [datetime[μs]]
1342
- [
1343
- 2001-01-01 00:00:00
1344
- 2001-01-02 00:00:00
1345
- 2001-01-03 00:00:00
1346
- ]
1347
- >>> date.dt.cast_time_unit("ms").alias("time_unit_ms")
1348
- shape: (3,)
1349
- Series: 'time_unit_ms' [datetime[ms]]
1350
- [
1351
- 2001-01-01 00:00:00
1352
- 2001-01-02 00:00:00
1353
- 2001-01-03 00:00:00
1354
- ]
1355
- >>> date.dt.cast_time_unit("ns").alias("time_unit_ns")
1356
- shape: (3,)
1357
- Series: 'time_unit_ns' [datetime[ns]]
1358
- [
1359
- 2001-01-01 00:00:00
1360
- 2001-01-02 00:00:00
1361
- 2001-01-03 00:00:00
1362
- ]
1363
- """
1364
-
1365
- def convert_time_zone(self, time_zone: str) -> Series:
1366
- """
1367
- Convert to given time zone for a Series of type Datetime.
1368
-
1369
- Parameters
1370
- ----------
1371
- time_zone
1372
- Time zone for the `Datetime` Series.
1373
-
1374
- Notes
1375
- -----
1376
- If converting from a time-zone-naive datetime, then conversion will happen
1377
- as if converting from UTC, regardless of your system's time zone.
1378
-
1379
- Examples
1380
- --------
1381
- >>> from datetime import datetime
1382
- >>> start = datetime(2020, 3, 1)
1383
- >>> stop = datetime(2020, 5, 1)
1384
- >>> date = pl.datetime_range(
1385
- ... start, stop, "1mo", time_zone="UTC", eager=True
1386
- ... ).alias("datetime")
1387
- >>> date
1388
- shape: (3,)
1389
- Series: 'datetime' [datetime[μs, UTC]]
1390
- [
1391
- 2020-03-01 00:00:00 UTC
1392
- 2020-04-01 00:00:00 UTC
1393
- 2020-05-01 00:00:00 UTC
1394
- ]
1395
- >>> date = date.dt.convert_time_zone("Europe/London").alias("London")
1396
- >>> date
1397
- shape: (3,)
1398
- Series: 'London' [datetime[μs, Europe/London]]
1399
- [
1400
- 2020-03-01 00:00:00 GMT
1401
- 2020-04-01 01:00:00 BST
1402
- 2020-05-01 01:00:00 BST
1403
- ]
1404
- """
1405
-
1406
- def replace_time_zone(
1407
- self,
1408
- time_zone: str | None,
1409
- *,
1410
- ambiguous: Ambiguous | Series = "raise",
1411
- non_existent: NonExistent = "raise",
1412
- ) -> Series:
1413
- """
1414
- Replace time zone for a Series of type Datetime.
1415
-
1416
- Different from `convert_time_zone`, this will also modify
1417
- the underlying timestamp and will ignore the original time zone.
1418
-
1419
- Parameters
1420
- ----------
1421
- time_zone
1422
- Time zone for the `Datetime` Series. Pass `None` to unset time zone.
1423
- ambiguous
1424
- Determine how to deal with ambiguous datetimes:
1425
-
1426
- - `'raise'` (default): raise
1427
- - `'earliest'`: use the earliest datetime
1428
- - `'latest'`: use the latest datetime
1429
- - `'null'`: set to null
1430
- non_existent
1431
- Determine how to deal with non-existent datetimes:
1432
-
1433
- - `'raise'` (default): raise
1434
- - `'null'`: set to null
1435
-
1436
- Examples
1437
- --------
1438
- >>> from datetime import datetime
1439
- >>> df = pl.DataFrame(
1440
- ... {
1441
- ... "london_timezone": pl.datetime_range(
1442
- ... datetime(2020, 3, 1),
1443
- ... datetime(2020, 7, 1),
1444
- ... "1mo",
1445
- ... time_zone="UTC",
1446
- ... eager=True,
1447
- ... )
1448
- ... .alias("datetime")
1449
- ... .dt.convert_time_zone(time_zone="Europe/London"),
1450
- ... }
1451
- ... )
1452
- >>> df.select(
1453
- ... [
1454
- ... pl.col("london_timezone"),
1455
- ... pl.col("london_timezone")
1456
- ... .dt.replace_time_zone(time_zone="Europe/Amsterdam")
1457
- ... .alias("London_to_Amsterdam"),
1458
- ... ]
1459
- ... )
1460
- shape: (5, 2)
1461
- ┌─────────────────────────────┬────────────────────────────────┐
1462
- │ london_timezone ┆ London_to_Amsterdam │
1463
- │ --- ┆ --- │
1464
- │ datetime[μs, Europe/London] ┆ datetime[μs, Europe/Amsterdam] │
1465
- ╞═════════════════════════════╪════════════════════════════════╡
1466
- │ 2020-03-01 00:00:00 GMT ┆ 2020-03-01 00:00:00 CET │
1467
- │ 2020-04-01 01:00:00 BST ┆ 2020-04-01 01:00:00 CEST │
1468
- │ 2020-05-01 01:00:00 BST ┆ 2020-05-01 01:00:00 CEST │
1469
- │ 2020-06-01 01:00:00 BST ┆ 2020-06-01 01:00:00 CEST │
1470
- │ 2020-07-01 01:00:00 BST ┆ 2020-07-01 01:00:00 CEST │
1471
- └─────────────────────────────┴────────────────────────────────┘
1472
-
1473
- You can use `ambiguous` to deal with ambiguous datetimes:
1474
-
1475
- >>> dates = [
1476
- ... "2018-10-28 01:30",
1477
- ... "2018-10-28 02:00",
1478
- ... "2018-10-28 02:30",
1479
- ... "2018-10-28 02:00",
1480
- ... ]
1481
- >>> df = pl.DataFrame(
1482
- ... {
1483
- ... "ts": pl.Series(dates).str.strptime(pl.Datetime),
1484
- ... "ambiguous": ["earliest", "earliest", "earliest", "latest"],
1485
- ... }
1486
- ... )
1487
- >>> df.with_columns(
1488
- ... ts_localized=pl.col("ts").dt.replace_time_zone(
1489
- ... "Europe/Brussels", ambiguous=pl.col("ambiguous")
1490
- ... )
1491
- ... )
1492
- shape: (4, 3)
1493
- ┌─────────────────────┬───────────┬───────────────────────────────┐
1494
- │ ts ┆ ambiguous ┆ ts_localized │
1495
- │ --- ┆ --- ┆ --- │
1496
- │ datetime[μs] ┆ str ┆ datetime[μs, Europe/Brussels] │
1497
- ╞═════════════════════╪═══════════╪═══════════════════════════════╡
1498
- │ 2018-10-28 01:30:00 ┆ earliest ┆ 2018-10-28 01:30:00 CEST │
1499
- │ 2018-10-28 02:00:00 ┆ earliest ┆ 2018-10-28 02:00:00 CEST │
1500
- │ 2018-10-28 02:30:00 ┆ earliest ┆ 2018-10-28 02:30:00 CEST │
1501
- │ 2018-10-28 02:00:00 ┆ latest ┆ 2018-10-28 02:00:00 CET │
1502
- └─────────────────────┴───────────┴───────────────────────────────┘
1503
- """
1504
-
1505
- def total_days(self) -> Series:
1506
- """
1507
- Extract the total days from a Duration type.
1508
-
1509
- Returns
1510
- -------
1511
- Series
1512
- Series of data type :class:`Int64`.
1513
-
1514
- Examples
1515
- --------
1516
- >>> from datetime import datetime
1517
- >>> date = pl.datetime_range(
1518
- ... datetime(2020, 3, 1), datetime(2020, 5, 1), "1mo", eager=True
1519
- ... ).alias("datetime")
1520
- >>> date
1521
- shape: (3,)
1522
- Series: 'datetime' [datetime[μs]]
1523
- [
1524
- 2020-03-01 00:00:00
1525
- 2020-04-01 00:00:00
1526
- 2020-05-01 00:00:00
1527
- ]
1528
- >>> date.diff().dt.total_days()
1529
- shape: (3,)
1530
- Series: 'datetime' [i64]
1531
- [
1532
- null
1533
- 31
1534
- 30
1535
- ]
1536
- """
1537
-
1538
- def total_hours(self) -> Series:
1539
- """
1540
- Extract the total hours from a Duration type.
1541
-
1542
- Returns
1543
- -------
1544
- Series
1545
- Series of data type :class:`Int64`.
1546
-
1547
- Examples
1548
- --------
1549
- >>> from datetime import datetime
1550
- >>> date = pl.datetime_range(
1551
- ... datetime(2020, 1, 1), datetime(2020, 1, 4), "1d", eager=True
1552
- ... ).alias("datetime")
1553
- >>> date
1554
- shape: (4,)
1555
- Series: 'datetime' [datetime[μs]]
1556
- [
1557
- 2020-01-01 00:00:00
1558
- 2020-01-02 00:00:00
1559
- 2020-01-03 00:00:00
1560
- 2020-01-04 00:00:00
1561
- ]
1562
- >>> date.diff().dt.total_hours()
1563
- shape: (4,)
1564
- Series: 'datetime' [i64]
1565
- [
1566
- null
1567
- 24
1568
- 24
1569
- 24
1570
- ]
1571
- """
1572
-
1573
- def total_minutes(self) -> Series:
1574
- """
1575
- Extract the total minutes from a Duration type.
1576
-
1577
- Returns
1578
- -------
1579
- Series
1580
- Series of data type :class:`Int64`.
1581
-
1582
- Examples
1583
- --------
1584
- >>> from datetime import datetime
1585
- >>> date = pl.datetime_range(
1586
- ... datetime(2020, 1, 1), datetime(2020, 1, 4), "1d", eager=True
1587
- ... ).alias("datetime")
1588
- >>> date
1589
- shape: (4,)
1590
- Series: 'datetime' [datetime[μs]]
1591
- [
1592
- 2020-01-01 00:00:00
1593
- 2020-01-02 00:00:00
1594
- 2020-01-03 00:00:00
1595
- 2020-01-04 00:00:00
1596
- ]
1597
- >>> date.diff().dt.total_minutes()
1598
- shape: (4,)
1599
- Series: 'datetime' [i64]
1600
- [
1601
- null
1602
- 1440
1603
- 1440
1604
- 1440
1605
- ]
1606
- """
1607
-
1608
- def total_seconds(self) -> Series:
1609
- """
1610
- Extract the total seconds from a Duration type.
1611
-
1612
- Returns
1613
- -------
1614
- Series
1615
- Series of data type :class:`Int64`.
1616
-
1617
- Examples
1618
- --------
1619
- >>> from datetime import datetime
1620
- >>> date = pl.datetime_range(
1621
- ... datetime(2020, 1, 1), datetime(2020, 1, 1, 0, 4, 0), "1m", eager=True
1622
- ... ).alias("datetime")
1623
- >>> date
1624
- shape: (5,)
1625
- Series: 'datetime' [datetime[μs]]
1626
- [
1627
- 2020-01-01 00:00:00
1628
- 2020-01-01 00:01:00
1629
- 2020-01-01 00:02:00
1630
- 2020-01-01 00:03:00
1631
- 2020-01-01 00:04:00
1632
- ]
1633
- >>> date.diff().dt.total_seconds()
1634
- shape: (5,)
1635
- Series: 'datetime' [i64]
1636
- [
1637
- null
1638
- 60
1639
- 60
1640
- 60
1641
- 60
1642
- ]
1643
- """
1644
-
1645
- def total_milliseconds(self) -> Series:
1646
- """
1647
- Extract the total milliseconds from a Duration type.
1648
-
1649
- Returns
1650
- -------
1651
- Series
1652
- Series of data type :class:`Int64`.
1653
-
1654
- Examples
1655
- --------
1656
- >>> from datetime import datetime
1657
- >>> date = pl.datetime_range(
1658
- ... datetime(2020, 1, 1),
1659
- ... datetime(2020, 1, 1, 0, 0, 1, 0),
1660
- ... "1ms",
1661
- ... eager=True,
1662
- ... ).alias("datetime")[:3]
1663
- >>> date
1664
- shape: (3,)
1665
- Series: 'datetime' [datetime[μs]]
1666
- [
1667
- 2020-01-01 00:00:00
1668
- 2020-01-01 00:00:00.001
1669
- 2020-01-01 00:00:00.002
1670
- ]
1671
- >>> date.diff().dt.total_milliseconds()
1672
- shape: (3,)
1673
- Series: 'datetime' [i64]
1674
- [
1675
- null
1676
- 1
1677
- 1
1678
- ]
1679
- """
1680
-
1681
- def total_microseconds(self) -> Series:
1682
- """
1683
- Extract the total microseconds from a Duration type.
1684
-
1685
- Returns
1686
- -------
1687
- Series
1688
- Series of data type :class:`Int64`.
1689
-
1690
- Examples
1691
- --------
1692
- >>> from datetime import datetime
1693
- >>> date = pl.datetime_range(
1694
- ... datetime(2020, 1, 1),
1695
- ... datetime(2020, 1, 1, 0, 0, 1, 0),
1696
- ... "1ms",
1697
- ... eager=True,
1698
- ... ).alias("datetime")[:3]
1699
- >>> date
1700
- shape: (3,)
1701
- Series: 'datetime' [datetime[μs]]
1702
- [
1703
- 2020-01-01 00:00:00
1704
- 2020-01-01 00:00:00.001
1705
- 2020-01-01 00:00:00.002
1706
- ]
1707
- >>> date.diff().dt.total_microseconds()
1708
- shape: (3,)
1709
- Series: 'datetime' [i64]
1710
- [
1711
- null
1712
- 1000
1713
- 1000
1714
- ]
1715
- """
1716
-
1717
- def total_nanoseconds(self) -> Series:
1718
- """
1719
- Extract the total nanoseconds from a Duration type.
1720
-
1721
- Returns
1722
- -------
1723
- Series
1724
- Series of data type :class:`Int64`.
1725
-
1726
- Examples
1727
- --------
1728
- >>> from datetime import datetime
1729
- >>> date = pl.datetime_range(
1730
- ... datetime(2020, 1, 1),
1731
- ... datetime(2020, 1, 1, 0, 0, 1, 0),
1732
- ... "1ms",
1733
- ... eager=True,
1734
- ... ).alias("datetime")[:3]
1735
- >>> date
1736
- shape: (3,)
1737
- Series: 'datetime' [datetime[μs]]
1738
- [
1739
- 2020-01-01 00:00:00
1740
- 2020-01-01 00:00:00.001
1741
- 2020-01-01 00:00:00.002
1742
- ]
1743
- >>> date.diff().dt.total_nanoseconds()
1744
- shape: (3,)
1745
- Series: 'datetime' [i64]
1746
- [
1747
- null
1748
- 1000000
1749
- 1000000
1750
- ]
1751
- """
1752
-
1753
- def offset_by(self, by: str | IntoExprColumn) -> Series:
1754
- """
1755
- Offset this date by a relative time offset.
1756
-
1757
- This differs from `pl.col("foo") + timedelta` in that it can
1758
- take months and leap years into account. Note that only a single minus
1759
- sign is allowed in the `by` string, as the first character.
1760
-
1761
- Parameters
1762
- ----------
1763
- by
1764
- The offset is dictated by the following string language:
1765
-
1766
- - 1ns (1 nanosecond)
1767
- - 1us (1 microsecond)
1768
- - 1ms (1 millisecond)
1769
- - 1s (1 second)
1770
- - 1m (1 minute)
1771
- - 1h (1 hour)
1772
- - 1d (1 calendar day)
1773
- - 1w (1 calendar week)
1774
- - 1mo (1 calendar month)
1775
- - 1q (1 calendar quarter)
1776
- - 1y (1 calendar year)
1777
-
1778
- By "calendar day", we mean the corresponding time on the next day
1779
- (which may not be 24 hours, due to daylight savings). Similarly for
1780
- "calendar week", "calendar month", "calendar quarter", and
1781
- "calendar year".
1782
-
1783
- Returns
1784
- -------
1785
- Series
1786
- Series of data type :class:`Date` or :class:`Datetime`.
1787
-
1788
- Examples
1789
- --------
1790
- >>> from datetime import datetime
1791
- >>> dates = pl.datetime_range(
1792
- ... datetime(2000, 1, 1), datetime(2005, 1, 1), "1y", eager=True
1793
- ... ).alias("datetime")
1794
- >>> dates
1795
- shape: (6,)
1796
- Series: 'datetime' [datetime[μs]]
1797
- [
1798
- 2000-01-01 00:00:00
1799
- 2001-01-01 00:00:00
1800
- 2002-01-01 00:00:00
1801
- 2003-01-01 00:00:00
1802
- 2004-01-01 00:00:00
1803
- 2005-01-01 00:00:00
1804
- ]
1805
- >>> dates.dt.offset_by("1y").alias("date_plus_1y")
1806
- shape: (6,)
1807
- Series: 'date_plus_1y' [datetime[μs]]
1808
- [
1809
- 2001-01-01 00:00:00
1810
- 2002-01-01 00:00:00
1811
- 2003-01-01 00:00:00
1812
- 2004-01-01 00:00:00
1813
- 2005-01-01 00:00:00
1814
- 2006-01-01 00:00:00
1815
- ]
1816
- >>> dates.dt.offset_by("-1y2mo").alias("date_minus_1y_2mon")
1817
- shape: (6,)
1818
- Series: 'date_minus_1y_2mon' [datetime[μs]]
1819
- [
1820
- 1998-11-01 00:00:00
1821
- 1999-11-01 00:00:00
1822
- 2000-11-01 00:00:00
1823
- 2001-11-01 00:00:00
1824
- 2002-11-01 00:00:00
1825
- 2003-11-01 00:00:00
1826
- ]
1827
- """
1828
-
1829
- def truncate(self, every: str | dt.timedelta | IntoExprColumn) -> Series:
1830
- """
1831
- Divide the date/ datetime range into buckets.
1832
-
1833
- Each date/datetime is mapped to the start of its bucket using the corresponding
1834
- local datetime. Note that:
1835
-
1836
- - Weekly buckets start on Monday.
1837
- - All other buckets start on the Unix epoch (1970-01-01).
1838
- - Ambiguous results are localised using the DST offset of the original
1839
- timestamp - for example, truncating `'2022-11-06 01:30:00 CST'` by
1840
- `'1h'` results in `'2022-11-06 01:00:00 CST'`, whereas truncating
1841
- `'2022-11-06 01:30:00 CDT'` by `'1h'` results in
1842
- `'2022-11-06 01:00:00 CDT'`.
1843
-
1844
- Parameters
1845
- ----------
1846
- every
1847
- The size of each bucket.
1848
-
1849
- Notes
1850
- -----
1851
- The `every` argument is created with the
1852
- the following string language:
1853
-
1854
- - 1ns (1 nanosecond)
1855
- - 1us (1 microsecond)
1856
- - 1ms (1 millisecond)
1857
- - 1s (1 second)
1858
- - 1m (1 minute)
1859
- - 1h (1 hour)
1860
- - 1d (1 calendar day)
1861
- - 1w (1 calendar week)
1862
- - 1mo (1 calendar month)
1863
- - 1q (1 calendar quarter)
1864
- - 1y (1 calendar year)
1865
-
1866
- By "calendar day", we mean the corresponding time on the next day (which may
1867
- not be 24 hours, due to daylight savings). Similarly for "calendar week",
1868
- "calendar month", "calendar quarter", and "calendar year".
1869
-
1870
- Returns
1871
- -------
1872
- Series
1873
- Series of data type :class:`Date` or :class:`Datetime`.
1874
-
1875
- Examples
1876
- --------
1877
- >>> from datetime import timedelta, datetime
1878
- >>> s = pl.datetime_range(
1879
- ... datetime(2001, 1, 1),
1880
- ... datetime(2001, 1, 2),
1881
- ... timedelta(minutes=165),
1882
- ... eager=True,
1883
- ... ).alias("datetime")
1884
- >>> s
1885
- shape: (9,)
1886
- Series: 'datetime' [datetime[μs]]
1887
- [
1888
- 2001-01-01 00:00:00
1889
- 2001-01-01 02:45:00
1890
- 2001-01-01 05:30:00
1891
- 2001-01-01 08:15:00
1892
- 2001-01-01 11:00:00
1893
- 2001-01-01 13:45:00
1894
- 2001-01-01 16:30:00
1895
- 2001-01-01 19:15:00
1896
- 2001-01-01 22:00:00
1897
- ]
1898
- >>> s.dt.truncate("1h")
1899
- shape: (9,)
1900
- Series: 'datetime' [datetime[μs]]
1901
- [
1902
- 2001-01-01 00:00:00
1903
- 2001-01-01 02:00:00
1904
- 2001-01-01 05:00:00
1905
- 2001-01-01 08:00:00
1906
- 2001-01-01 11:00:00
1907
- 2001-01-01 13:00:00
1908
- 2001-01-01 16:00:00
1909
- 2001-01-01 19:00:00
1910
- 2001-01-01 22:00:00
1911
- ]
1912
-
1913
- >>> s = pl.datetime_range(
1914
- ... datetime(2001, 1, 1), datetime(2001, 1, 1, 1), "10m", eager=True
1915
- ... ).alias("datetime")
1916
- >>> s
1917
- shape: (7,)
1918
- Series: 'datetime' [datetime[μs]]
1919
- [
1920
- 2001-01-01 00:00:00
1921
- 2001-01-01 00:10:00
1922
- 2001-01-01 00:20:00
1923
- 2001-01-01 00:30:00
1924
- 2001-01-01 00:40:00
1925
- 2001-01-01 00:50:00
1926
- 2001-01-01 01:00:00
1927
- ]
1928
- >>> s.dt.truncate("30m")
1929
- shape: (7,)
1930
- Series: 'datetime' [datetime[μs]]
1931
- [
1932
- 2001-01-01 00:00:00
1933
- 2001-01-01 00:00:00
1934
- 2001-01-01 00:00:00
1935
- 2001-01-01 00:30:00
1936
- 2001-01-01 00:30:00
1937
- 2001-01-01 00:30:00
1938
- 2001-01-01 01:00:00
1939
- ]
1940
- """
1941
-
1942
- def round(self, every: str | dt.timedelta | IntoExprColumn) -> Series:
1943
- """
1944
- Divide the date/ datetime range into buckets.
1945
-
1946
- - Each date/datetime in the first half of the interval
1947
- is mapped to the start of its bucket.
1948
- - Each date/datetime in the second half of the interval
1949
- is mapped to the end of its bucket.
1950
- - Half-way points are mapped to the start of their bucket.
1951
-
1952
- Ambiguous results are localized using the DST offset of the original timestamp -
1953
- for example, rounding `'2022-11-06 01:20:00 CST'` by `'1h'` results in
1954
- `'2022-11-06 01:00:00 CST'`, whereas rounding `'2022-11-06 01:20:00 CDT'` by
1955
- `'1h'` results in `'2022-11-06 01:00:00 CDT'`.
1956
-
1957
- Parameters
1958
- ----------
1959
- every
1960
- Every interval start and period length
1961
-
1962
- Returns
1963
- -------
1964
- Series
1965
- Series of data type :class:`Date` or :class:`Datetime`.
1966
-
1967
- Notes
1968
- -----
1969
- The `every` argument is created with the
1970
- the following string language:
1971
-
1972
- - 1ns (1 nanosecond)
1973
- - 1us (1 microsecond)
1974
- - 1ms (1 millisecond)
1975
- - 1s (1 second)
1976
- - 1m (1 minute)
1977
- - 1h (1 hour)
1978
- - 1d (1 calendar day)
1979
- - 1w (1 calendar week)
1980
- - 1mo (1 calendar month)
1981
- - 1q (1 calendar quarter)
1982
- - 1y (1 calendar year)
1983
-
1984
- By "calendar day", we mean the corresponding time on the next day (which may
1985
- not be 24 hours, due to daylight savings). Similarly for "calendar week",
1986
- "calendar month", "calendar quarter", and "calendar year".
1987
-
1988
- Examples
1989
- --------
1990
- >>> from datetime import timedelta, datetime
1991
- >>> start = datetime(2001, 1, 1)
1992
- >>> stop = datetime(2001, 1, 2)
1993
- >>> s = pl.datetime_range(
1994
- ... start, stop, timedelta(minutes=165), eager=True
1995
- ... ).alias("datetime")
1996
- >>> s
1997
- shape: (9,)
1998
- Series: 'datetime' [datetime[μs]]
1999
- [
2000
- 2001-01-01 00:00:00
2001
- 2001-01-01 02:45:00
2002
- 2001-01-01 05:30:00
2003
- 2001-01-01 08:15:00
2004
- 2001-01-01 11:00:00
2005
- 2001-01-01 13:45:00
2006
- 2001-01-01 16:30:00
2007
- 2001-01-01 19:15:00
2008
- 2001-01-01 22:00:00
2009
- ]
2010
- >>> s.dt.round("1h")
2011
- shape: (9,)
2012
- Series: 'datetime' [datetime[μs]]
2013
- [
2014
- 2001-01-01 00:00:00
2015
- 2001-01-01 03:00:00
2016
- 2001-01-01 06:00:00
2017
- 2001-01-01 08:00:00
2018
- 2001-01-01 11:00:00
2019
- 2001-01-01 14:00:00
2020
- 2001-01-01 17:00:00
2021
- 2001-01-01 19:00:00
2022
- 2001-01-01 22:00:00
2023
- ]
2024
- >>> round_str = s.dt.round("1h")
2025
- >>> round_td = s.dt.round(timedelta(hours=1))
2026
- >>> round_str.equals(round_td)
2027
- True
2028
-
2029
- >>> start = datetime(2001, 1, 1)
2030
- >>> stop = datetime(2001, 1, 1, 1)
2031
- >>> s = pl.datetime_range(start, stop, "10m", eager=True).alias("datetime")
2032
- >>> s.dt.round("30m")
2033
- shape: (7,)
2034
- Series: 'datetime' [datetime[μs]]
2035
- [
2036
- 2001-01-01 00:00:00
2037
- 2001-01-01 00:00:00
2038
- 2001-01-01 00:30:00
2039
- 2001-01-01 00:30:00
2040
- 2001-01-01 00:30:00
2041
- 2001-01-01 01:00:00
2042
- 2001-01-01 01:00:00
2043
- ]
2044
- """
2045
-
2046
- def combine(self, time: dt.time | Series, time_unit: TimeUnit = "us") -> Series:
2047
- """
2048
- Create a naive Datetime from an existing Date/Datetime expression and a Time.
2049
-
2050
- If the underlying expression is a Datetime then its time component is replaced,
2051
- and if it is a Date then a new Datetime is created by combining the two values.
2052
-
2053
- Parameters
2054
- ----------
2055
- time
2056
- A python time literal or Series of the same length as this Series.
2057
- time_unit : {'ns', 'us', 'ms'}
2058
- Unit of time.
2059
-
2060
- Examples
2061
- --------
2062
- >>> from datetime import datetime, time
2063
- >>> s = pl.Series(
2064
- ... "dtm",
2065
- ... [datetime(2022, 12, 31, 10, 30, 45), datetime(2023, 7, 5, 23, 59, 59)],
2066
- ... )
2067
- >>> s.dt.combine(time(1, 2, 3, 456000))
2068
- shape: (2,)
2069
- Series: 'dtm' [datetime[μs]]
2070
- [
2071
- 2022-12-31 01:02:03.456
2072
- 2023-07-05 01:02:03.456
2073
- ]
2074
- """
2075
-
2076
- def month_start(self) -> Series:
2077
- """
2078
- Roll backward to the first day of the month.
2079
-
2080
- Returns
2081
- -------
2082
- Series
2083
- Series of data type :class:`Date` or :class:`Datetime`.
2084
-
2085
- Notes
2086
- -----
2087
- If you're coming from pandas, you can think of this as a vectorised version
2088
- of `pandas.tseries.offsets.MonthBegin().rollback(datetime)`.
2089
-
2090
- Examples
2091
- --------
2092
- >>> from datetime import datetime
2093
- >>> s = pl.datetime_range(
2094
- ... datetime(2000, 1, 2, 2), datetime(2000, 4, 2, 2), "1mo", eager=True
2095
- ... ).alias("datetime")
2096
- >>> s.dt.month_start()
2097
- shape: (4,)
2098
- Series: 'datetime' [datetime[μs]]
2099
- [
2100
- 2000-01-01 02:00:00
2101
- 2000-02-01 02:00:00
2102
- 2000-03-01 02:00:00
2103
- 2000-04-01 02:00:00
2104
- ]
2105
- """
2106
-
2107
- def month_end(self) -> Series:
2108
- """
2109
- Roll forward to the last day of the month.
2110
-
2111
- Returns
2112
- -------
2113
- Series
2114
- Series of data type :class:`Date` or :class:`Datetime`.
2115
-
2116
- Notes
2117
- -----
2118
- If you're coming from pandas, you can think of this as a vectorised version
2119
- of `pandas.tseries.offsets.MonthEnd().rollforward(datetime)`.
2120
-
2121
- Examples
2122
- --------
2123
- >>> from datetime import datetime
2124
- >>> s = pl.datetime_range(
2125
- ... datetime(2000, 1, 2, 2), datetime(2000, 4, 2, 2), "1mo", eager=True
2126
- ... ).alias("datetime")
2127
- >>> s.dt.month_end()
2128
- shape: (4,)
2129
- Series: 'datetime' [datetime[μs]]
2130
- [
2131
- 2000-01-31 02:00:00
2132
- 2000-02-29 02:00:00
2133
- 2000-03-31 02:00:00
2134
- 2000-04-30 02:00:00
2135
- ]
2136
- """
2137
-
2138
- def base_utc_offset(self) -> Series:
2139
- """
2140
- Base offset from UTC.
2141
-
2142
- This is usually constant for all datetimes in a given time zone, but
2143
- may vary in the rare case that a country switches time zone, like
2144
- Samoa (Apia) did at the end of 2011.
2145
-
2146
- Returns
2147
- -------
2148
- Series
2149
- Series of data type :class:`Duration`.
2150
-
2151
- See Also
2152
- --------
2153
- Series.dt.dst_offset : Additional offset currently in effect.
2154
-
2155
- Examples
2156
- --------
2157
- >>> from datetime import datetime
2158
- >>> s = pl.datetime_range(
2159
- ... datetime(2011, 12, 29),
2160
- ... datetime(2012, 1, 1),
2161
- ... "2d",
2162
- ... time_zone="Pacific/Apia",
2163
- ... eager=True,
2164
- ... ).alias("datetime")
2165
- >>> s
2166
- shape: (2,)
2167
- Series: 'datetime' [datetime[μs, Pacific/Apia]]
2168
- [
2169
- 2011-12-29 00:00:00 -10
2170
- 2011-12-31 00:00:00 +14
2171
- ]
2172
- >>> s.dt.base_utc_offset()
2173
- shape: (2,)
2174
- Series: 'datetime' [duration[ms]]
2175
- [
2176
- -11h
2177
- 13h
2178
- ]
2179
- """
2180
-
2181
- def dst_offset(self) -> Series:
2182
- """
2183
- Additional offset currently in effect (typically due to daylight saving time).
2184
-
2185
- Returns
2186
- -------
2187
- Series
2188
- Series of data type :class:`Duration`.
2189
-
2190
- See Also
2191
- --------
2192
- Series.dt.base_utc_offset : Base offset from UTC.
2193
-
2194
- Examples
2195
- --------
2196
- >>> from datetime import datetime
2197
- >>> s = pl.datetime_range(
2198
- ... datetime(2020, 10, 25),
2199
- ... datetime(2020, 10, 26),
2200
- ... time_zone="Europe/London",
2201
- ... eager=True,
2202
- ... ).alias("datetime")
2203
- >>> s
2204
- shape: (2,)
2205
- Series: 'datetime' [datetime[μs, Europe/London]]
2206
- [
2207
- 2020-10-25 00:00:00 BST
2208
- 2020-10-26 00:00:00 GMT
2209
- ]
2210
- >>> s.dt.dst_offset()
2211
- shape: (2,)
2212
- Series: 'datetime' [duration[ms]]
2213
- [
2214
- 1h
2215
- 0ms
2216
- ]
2217
- """
2218
-
2219
- def replace(
2220
- self,
2221
- *,
2222
- year: int | Series | None = None,
2223
- month: int | Series | None = None,
2224
- day: int | Series | None = None,
2225
- hour: int | Series | None = None,
2226
- minute: int | Series | None = None,
2227
- second: int | Series | None = None,
2228
- microsecond: int | Series | None = None,
2229
- ambiguous: Ambiguous | Series = "raise",
2230
- ) -> Series:
2231
- """
2232
- Replace time unit.
2233
-
2234
- Parameters
2235
- ----------
2236
- year
2237
- Literal or Series.
2238
- month
2239
- Literal or Series, ranging from 1-12.
2240
- day
2241
- Literal or Series, ranging from 1-31.
2242
- hour
2243
- Literal or Series, ranging from 0-23.
2244
- minute
2245
- Literal or Series, ranging from 0-59.
2246
- second
2247
- Literal or Series, ranging from 0-59.
2248
- microsecond
2249
- Literal or Series, ranging from 0-999999.
2250
- ambiguous
2251
- Determine how to deal with ambiguous datetimes:
2252
-
2253
- - `'raise'` (default): raise
2254
- - `'earliest'`: use the earliest datetime
2255
- - `'latest'`: use the latest datetime
2256
- - `'null'`: set to null
2257
-
2258
- Returns
2259
- -------
2260
- Series
2261
- Series of data type :class:`Date` or :class:`Datetime` with the specified
2262
- time units replaced.
2263
-
2264
- Examples
2265
- --------
2266
- >>> from datetime import date
2267
- >>> s = pl.Series("date", [date(2013, 1, 1), date(2024, 1, 2)])
2268
- >>> s.dt.replace(year=1800)
2269
- shape: (2,)
2270
- Series: 'date' [date]
2271
- [
2272
- 1800-01-01
2273
- 1800-01-02
2274
- ]
2275
- """