Polars utils module¶
Functions¶
iso_week_date.polars_utils.datetime_to_isoweek ¶
Converts date(time)
series/expr
to str
values representing ISO Week format YYYY-WNN.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
series or expr of |
required |
offset
|
OffsetType
|
offset in days or |
timedelta(days=0)
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr with converted ISO Week values (in format YYYY-WNN) |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the following condition is met:
|
Examples:
>>> from datetime import date, timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import datetime_to_isoweek
>>>
>>> s = pl.date_range(date(2023, 1, 1), date(2023, 1, 5), interval="1d", eager=True)
>>> datetime_to_isoweek(s, offset=timedelta(days=1))
shape: (5,)
Series: 'literal' [str]
[
"2022-W52"
"2022-W52"
"2023-W01"
"2023-W01"
"2023-W01"
]
>>> df = pl.DataFrame({"date": s})
>>> df.select(datetime_to_isoweek(pl.col("date"), offset=1))
shape: (5, 1)
┌──────────┐
│ date │
│ --- │
│ str │
╞══════════╡
│ 2022-W52 │
│ 2022-W52 │
│ 2023-W01 │
│ 2023-W01 │
│ 2023-W01 │
└──────────┘
Source code in iso_week_date/polars_utils.py
iso_week_date.polars_utils.datetime_to_isoweekdate ¶
Converts date(time)
series/expr
to str
values representing ISO Week date format YYYY-WNN-D.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
series or expr of |
required |
offset
|
OffsetType
|
offset in days or |
timedelta(days=0)
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr with converted ISO Week values (in format YYYY-WNN-D) |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the following condition is met:
|
Examples:
>>> from datetime import date, timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import datetime_to_isoweekdate
>>>
>>> s = pl.date_range(date(2023, 1, 1), date(2023, 1, 5), interval="1d", eager=True)
>>> datetime_to_isoweekdate(s, offset=timedelta(days=1))
shape: (5,)
Series: 'literal' [str]
[
"2022-W52-6"
"2022-W52-7"
"2023-W01-1"
"2023-W01-2"
"2023-W01-3"
]
>>> df = pl.DataFrame({"date": s})
>>> df.select(datetime_to_isoweekdate(pl.col("date"), offset=1))
shape: (5, 1)
┌────────────┐
│ date │
│ --- │
│ str │
╞════════════╡
│ 2022-W52-6 │
│ 2022-W52-7 │
│ 2023-W01-1 │
│ 2023-W01-2 │
│ 2023-W01-3 │
└────────────┘
Source code in iso_week_date/polars_utils.py
iso_week_date.polars_utils.isoweek_to_datetime ¶
isoweek_to_datetime(
series: T,
offset: OffsetType = timedelta(days=0),
weekday: int = 1,
*,
strict: bool = True,
) -> T
Converts series or expr of str
values in ISO Week format YYYY-WNN to a series or expr of pl.Date
values.
offset
represents how many days to add to the date before converting to pl.Date
, and it can be negative.
weekday
represents the weekday to use for conversion in ISO Week format (1-7), where 1 is the first day of the
week, 7 is the last one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
Series or Expr of |
required |
offset
|
OffsetType
|
Offset in days or |
timedelta(days=0)
|
weekday
|
int
|
Weekday to use for conversion (1-7) |
1
|
strict
|
bool
|
Raise an error if the values cannot be converted to datetime. Otherwise mask out with a null value. |
True
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr of converted date values |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the following condition is met:
|
ValueError
|
If |
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import isoweek_to_datetime
>>>
>>> s = pl.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> isoweek_to_datetime(series=s, offset=timedelta(days=1))
shape: (3,)
Series: '' [date]
[
2022-12-27
2023-01-03
2023-01-10
]
Source code in iso_week_date/polars_utils.py
iso_week_date.polars_utils.isoweekdate_to_datetime ¶
isoweekdate_to_datetime(
series: T, offset: OffsetType = timedelta(days=0), *, strict: bool = True
) -> T
Converts series/expr
of values in ISO Week date format YYYY-WNN-D to a series or expr of pl.Date
values.
offset
represents how many days to add to the date before converting to pl.Date
, and it can be negative.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
Series or Expr of |
required |
offset
|
OffsetType
|
Offset in days or |
timedelta(days=0)
|
strict
|
bool
|
Raise an error if the values cannot be converted to datetime. Otherwise mask out with a null value. |
True
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr of converted date values |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the following condition is met:
|
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import isoweekdate_to_datetime
>>>
>>> s = pl.Series(["2022-W52-7", "2023-W01-1", "2023-W02-1"])
>>> isoweekdate_to_datetime(series=s, offset=timedelta(days=1))
shape: (3,)
Series: '' [date]
[
2023-01-02
2023-01-03
2023-01-10
]
Source code in iso_week_date/polars_utils.py
iso_week_date.polars_utils.is_isoweek_series ¶
Checks if a series or expr contains only values in ISO Week format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
series or expr of |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Raises:
Type | Description |
---|---|
TypeError
|
If |
Examples:
>>> import polars as pl
>>> from iso_week_date.polars_utils import is_isoweek_series
>>>
>>> s = pl.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> is_isoweek_series(s)
True
Source code in iso_week_date/polars_utils.py
iso_week_date.polars_utils.is_isoweekdate_series ¶
Checks if a series or expr contains only values in ISO Week date format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
series or expr of |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Raises:
Type | Description |
---|---|
TypeError
|
If |
Examples:
>>> import polars as pl
>>> from iso_week_date.polars_utils import is_isoweekdate_series
>>>
>>> s = pl.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> is_isoweekdate_series(series=s)
True
Source code in iso_week_date/polars_utils.py
Series extension¶
iso_week_date.polars_utils.SeriesIsoWeek ¶
Bases: Generic[T]
Polars Series and Expr extension that provides methods for working with ISO weeks and dates.
Instead of importing and working with single functions from the polars_utils
module, it is possible to import the
Series and Expr extension class to be able to
use the functions as methods on Series and Expr objects.
To accomplish this, it is enough to load SeriesIsoWeek
into scope:
from datetime import date, timedelta
import polars as pl
from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
s = pl.date_range(date(2023, 1, 1), date(2023, 1, 10), interval="1d")
s.iwd.datetime_to_isoweek(offset=timedelta(days=1))
df = pl.DataFrame({"date": s})
df.select(pl.col("date").iwd.datetime_to_isoweek(offset=1))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
T
|
The pandas Series object the extension is attached to. |
required |
Attributes:
Name | Type | Description |
---|---|---|
_series |
T
|
The pandas Series object the extension is attached to. |
Source code in iso_week_date/polars_utils.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
|
datetime_to_isoweek ¶
Converts date(time)
series/expr
to str
values representing ISO Week format YYYY-WNN.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset
|
OffsetType
|
offset in days or |
timedelta(0)
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr with converted ISO Week values (in format YYYY-WNN) |
Raises:
Type | Description |
---|---|
TypeError
|
If |
Examples:
>>> from datetime import date, timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.date_range(date(2023, 1, 1), date(2023, 1, 5), interval="1d", eager=True)
>>> s.iwd.datetime_to_isoweek(offset=timedelta(days=1))
shape: (5,)
Series: 'literal' [str]
[
"2022-W52"
"2022-W52"
"2023-W01"
"2023-W01"
"2023-W01"
]
>>> df = pl.DataFrame({"date": s})
>>> df.select(pl.col("date").iwd.datetime_to_isoweek(offset=1))
shape: (5, 1)
┌──────────┐
│ date │
│ --- │
│ str │
╞══════════╡
│ 2022-W52 │
│ 2022-W52 │
│ 2023-W01 │
│ 2023-W01 │
│ 2023-W01 │
└──────────┘
Source code in iso_week_date/polars_utils.py
datetime_to_isoweekdate ¶
Converts date(time)
series/expr
to str
values representing ISO Week date format YYYY-WNN-D.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset
|
OffsetType
|
offset in days or |
timedelta(0)
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr with converted ISO Week values (in format YYYY-WNN-D) |
Raises:
Type | Description |
---|---|
TypeError
|
If |
Examples:
>>> from datetime import date, timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.date_range(date(2023, 1, 1), date(2023, 1, 5), interval="1d", eager=True)
>>> s.iwd.datetime_to_isoweekdate(offset=timedelta(days=1))
shape: (5,)
Series: 'literal' [str]
[
"2022-W52-6"
"2022-W52-7"
"2023-W01-1"
"2023-W01-2"
"2023-W01-3"
]
>>> df = pl.DataFrame({"date": s})
>>> df.select(pl.col("date").iwd.datetime_to_isoweekdate(offset=1))
shape: (5, 1)
┌────────────┐
│ date │
│ --- │
│ str │
╞════════════╡
│ 2022-W52-6 │
│ 2022-W52-7 │
│ 2023-W01-1 │
│ 2023-W01-2 │
│ 2023-W01-3 │
└────────────┘
Source code in iso_week_date/polars_utils.py
is_isoweek ¶
Checks if a series or expr contains only values in ISO Week format.
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> s.iwd.is_isoweek()
True
Source code in iso_week_date/polars_utils.py
is_isoweekdate ¶
Checks if a series or expr contains only values in ISO Week date format.
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> s.iwd.is_isoweekdate()
True
Source code in iso_week_date/polars_utils.py
isoweek_to_datetime ¶
isoweek_to_datetime(
offset: OffsetType = timedelta(0), weekday: int = 1, *, strict: bool = True
) -> T
Converts series or expr of str
values in ISO Week format YYYY-WNN to a series or expr of pl.Date
values.
offset
represents how many days to add to the date before converting to pl.Date
, and it can be negative.
weekday
represents the weekday to use for conversion in ISO Week format (1-7), where 1 is the first day of the
week, 7 is the last one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset
|
OffsetType
|
Offset in days or |
timedelta(0)
|
weekday
|
int
|
Weekday to use for conversion (1-7). |
1
|
strict
|
bool
|
Raise an error if the values cannot be converted to datetime. Otherwise mask out with a null value. |
True
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr of converted date values |
Raises:
Type | Description |
---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> s.iwd.isoweek_to_datetime(offset=timedelta(days=1))
shape: (3,)
Series: '' [date]
[
2022-12-27
2023-01-03
2023-01-10
]
Source code in iso_week_date/polars_utils.py
isoweekdate_to_datetime ¶
Converts str
series or expr of ISO Week date format YYYY-WNN-D to a series or expr of pl.Date
values.
offset
represents how many days to add to the date before converting to pl.Date
, and it can be negative.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset
|
OffsetType
|
Offset in days or |
timedelta(0)
|
strict
|
bool
|
Raise an error if the values cannot be converted to datetime. Otherwise mask out with a null value. |
True
|
Returns:
Type | Description |
---|---|
T
|
Series or Expr of converted date values |
Raises:
Type | Description |
---|---|
TypeError
|
If |
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pl.Series(["2022-W52-7", "2023-W01-1", "2023-W02-1"])
>>> s.iwd.isoweekdate_to_datetime(offset=timedelta(days=1))
shape: (3,)
Series: '' [date]
[
2023-01-02
2023-01-03
2023-01-10
]