Pandas utils module
Functions
iso_week_date.pandas_utils.datetime_to_isoweek
datetime_to_isoweek(series: Series[Timestamp], offset: OffsetType = 0) -> pd.Series[str]
Converts series of date or datetime values to str values representing ISO Week format YYYY-WNN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[Timestamp]
|
series of |
required |
offset
|
OffsetType
|
offset in days or |
0
|
Returns:
| Type | Description |
|---|---|
Series[str]
|
Series 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
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import datetime_to_isoweek
>>>
>>> s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1D"))
>>> datetime_to_isoweek(series=s, offset=pd.Timedelta(days=1)).to_list()
['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02']
Source code in src/iso_week_date/pandas_utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
iso_week_date.pandas_utils.datetime_to_isoweekdate
datetime_to_isoweekdate(series: Series[Timestamp], offset: OffsetType = 0) -> pd.Series[str]
Converts series of date or datetime values to str values representing ISO Week date format YYYY-WNN-D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[Timestamp]
|
series of |
required |
offset
|
OffsetType
|
offset in days or |
0
|
Returns:
| Type | Description |
|---|---|
Series[str]
|
Series with converted ISO Week date values (in format YYYY-WNN-D) |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any of the following condition is met:
|
Examples:
>>> from datetime import date
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import datetime_to_isoweekdate
>>>
>>> s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1D"))
>>> datetime_to_isoweekdate(series=s, offset=pd.Timedelta(days=1)).to_list()
['2022-W52-6', '2022-W52-7', '2023-W01-1',..., '2023-W01-7', '2023-W02-1']
Source code in src/iso_week_date/pandas_utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
iso_week_date.pandas_utils.isoweek_to_datetime
isoweek_to_datetime(series: Series[str], offset: OffsetType = 0, weekday: int = 1, *, strict: bool = True) -> pd.Series[pd.Timestamp]
Converts series of str values in ISO Week format to a series of datetime values.
offset represents how many days to add to the date before converting to datetime 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
|
Series[str]
|
Series of |
required |
offset
|
OffsetType
|
Offset in days or pd.Timedelta. It represents how many days to add to the date before converting to IsoWeek, it can be negative. |
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 |
|---|---|
Series[Timestamp]
|
Series of converted datetime values |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any of the following condition is met:
|
ValueError
|
If |
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import isoweek_to_datetime
>>>
>>> s = pd.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> isoweek_to_datetime(series=s, offset=pd.Timedelta(days=1))
0 2022-12-27
1 2023-01-03
2 2023-01-10
dtype: datetime64[...]
Source code in src/iso_week_date/pandas_utils.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
iso_week_date.pandas_utils.isoweekdate_to_datetime
isoweekdate_to_datetime(series: Series[str], offset: OffsetType = 0, *, strict: bool = True) -> pd.Series[pd.Timestamp]
Converts series of str values in ISO Week date format to a series of datetime values.
offset represents how many days to add to the date before converting to datetime and it can be negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[str]
|
series of |
required |
offset
|
OffsetType
|
offset in days or pd.Timedelta. It represents how many days to add to the date before converting to IsoWeek, it can be negative. |
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 |
|---|---|
Series[Timestamp]
|
Series of converted datetime values |
Raises:
| Type | Description |
|---|---|
TypeError
|
If one of the following condition is met:
|
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import isoweekdate_to_datetime
>>>
>>> s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> isoweekdate_to_datetime(series=s, offset=pd.Timedelta(days=1))
0 2022-12-27
1 2023-01-03
2 2023-01-10
dtype: datetime64[...]
Source code in src/iso_week_date/pandas_utils.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
iso_week_date.pandas_utils.is_isoweek_series
is_isoweek_series(series: Series[str]) -> bool
Checks if series contains only values in ISO Week format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[str]
|
series of |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import is_isoweek_series
>>>
>>> s = pd.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> is_isoweek_series(series=s)
True
Source code in src/iso_week_date/pandas_utils.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
iso_week_date.pandas_utils.is_isoweekdate_series
is_isoweekdate_series(series: Series[str]) -> bool
Checks if series contains only values in ISO Week date format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[str]
|
series of |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import is_isoweekdate_series
>>> s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> is_isoweekdate_series(series=s)
True
Source code in src/iso_week_date/pandas_utils.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
Series extension
iso_week_date.pandas_utils.SeriesIsoWeek
Pandas Series extension that provides methods for working with ISO weeks and dates.
Instead of importing and working with single functions from the pandas_utils module, it is possible to import the
Series extension class to be able to use the functions
as methods on Series objects.
To accomplish this, it is enough to load SeriesIsoWeek into scope:
from datetime import date
import pandas as pd
from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1D"))
s.iwd.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list()
# ['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02']
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series[str] | Series[Timestamp]
|
The pandas Series object the extension is attached to. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
_series |
The pandas Series object the extension is attached to. |
Source code in src/iso_week_date/pandas_utils.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 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 | |
datetime_to_isoweek
datetime_to_isoweek(offset: OffsetType = 0) -> pd.Series[str]
Converts series of date or datetime values to str values representing ISO Week format YYYY-WNN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
OffsetType
|
offset in days or |
0
|
Returns:
| Type | Description |
|---|---|
Series[str]
|
ISO Week pandas series in format YYYY-WNN |
Raises:
| Type | Description |
|---|---|
TypeError
|
If series values are not |
Examples:
>>> from datetime import date
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1D"))
>>> s.iwd.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list()
['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02']
Source code in src/iso_week_date/pandas_utils.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
datetime_to_isoweekdate
datetime_to_isoweekdate(offset: OffsetType = 0) -> pd.Series[str]
Converts series of date or datetime values to str values representing ISO Week date format YYYY-WNN-D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
OffsetType
|
offset in days or |
0
|
Returns:
| Type | Description |
|---|---|
Series[str]
|
ISO Week date pandas series in format YYYY-WNN-D |
Raises:
| Type | Description |
|---|---|
TypeError
|
If series values are not |
Examples:
>>> from datetime import date
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1D"))
>>> s.iwd.datetime_to_isoweekdate(offset=pd.Timedelta(days=1)).to_list()
['2022-W52-6', '2022-W52-7', '2023-W01-1',..., '2023-W01-7', '2023-W02-1']
Source code in src/iso_week_date/pandas_utils.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
is_isoweek
is_isoweek() -> bool
Checks if series contains only values in ISO Week format.
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> s.iwd.is_isoweek()
True
Source code in src/iso_week_date/pandas_utils.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
is_isoweekdate
is_isoweekdate() -> bool
Checks if series contains only values in ISO Week date format.
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> s.iwd.is_isoweekdate()
True
Source code in src/iso_week_date/pandas_utils.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | |
isoweek_to_datetime
isoweek_to_datetime(offset: OffsetType = 0, weekday: int = 1, *, strict: bool = True) -> pd.Series[pd.Timestamp]
Converts series of str values in ISO Week format to a series of datetime values.
offset represents how many days to add to the date before converting to datetime 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 pd.Timedelta. It represents how many days to add to the date before converting to IsoWeek, it can be negative. |
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 |
|---|---|
Series[Timestamp]
|
Series of converted datetime values |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(["2022-W52", "2023-W01", "2023-W02"])
>>> s.iwd.isoweek_to_datetime(offset=pd.Timedelta(days=1))
0 2022-12-27
1 2023-01-03
2 2023-01-10
dtype: datetime64[...]
Source code in src/iso_week_date/pandas_utils.py
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 | |
isoweekdate_to_datetime
isoweekdate_to_datetime(offset: OffsetType = 0, *, strict: bool = True) -> pd.Series[pd.Timestamp]
Converts series of str values in ISO Week date format to a series of datetime values.
offset represents how many days to add to the date before converting to datetime and it can be negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
OffsetType
|
Offset in days or pd.Timedelta. It represents how many days to add to the date before converting to IsoWeek, it can be negative. |
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 |
|---|---|
Series[Timestamp]
|
Series of converted datetime values |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
>>> import pandas as pd
>>> from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401
>>>
>>> s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"])
>>> s.iwd.isoweekdate_to_datetime(offset=pd.Timedelta(days=1))
0 2022-12-27
1 2023-01-03
2 2023-01-10
dtype: datetime64[...]
Source code in src/iso_week_date/pandas_utils.py
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 | |