{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Temporal\n", "\n", "[[Polars Documentation](https://docs.pola.rs/api/python/stable/reference/expressions/temporal.html)]\n", "\n", "OpenDP supports some manipulation of dates and times, \n", "which can be useful in predicates and grouping functions." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import polars as pl\n", "\n", "import opendp.prelude as dp\n", "dp.enable_features(\"contrib\")\n", "# Fetch and unpack the data. \n", "![ -e ../sample_FR_LFS.csv ] || ( curl 'https://github.com/opendp/dp-test-datasets/blob/main/data/sample_FR_LFS.csv.zip?raw=true' --location --output sample_FR_LFS.csv.zip; unzip sample_FR_LFS.csv.zip -d ../ )\n", "\n", "lf_dates = (\n", " pl.scan_csv(\"../sample_FR_LFS.csv\", ignore_errors=True)\n", " # prepare the data with some expressions that are not yet supported in OpenDP\n", " .select(DATE=pl.concat_str(\"REFYEAR\", pl.col.QUARTER * 3, pl.lit(\"01\"), separator=\"-\"))\n", ")\n", "\n", "context = dp.Context.compositor(\n", " data=lf_dates,\n", " privacy_unit=dp.unit_of(contributions=36),\n", " privacy_loss=dp.loss_of(epsilon=1.0, delta=1e-7),\n", " split_evenly_over=1,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Date/Time Components\n", "\n", "- Date expressions (can be applied to `pl.Date` and `pl.Datetime` dtypes)\n", " - `.dt.year`\n", " - `.dt.iso_year`\n", " - `.dt.quarter`\n", " - `.dt.month`\n", " - `.dt.week`\n", " - `.dt.weekday`\n", " - `.dt.day`\n", " - `.dt.ordinal_day`\n", "- Time expressions (can be applied to `pl.Time` and `pl.Datetime` dtypes)\n", " - `.dt.hour`\n", " - `.dt.minute`\n", " - `.dt.second`\n", " - `.dt.millisecond`\n", " - `.dt.microsecond`\n", " - `.dt.nanosecond`\n", "\n", "An example of their use can be seen below, where a string column is parsed into dates,\n", "and then year and month components are retrieved from the dates." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
YEAR | MONTH | len |
---|---|---|
i32 | i8 | u32 |
2004 | 3 | 4209 |
2004 | 6 | 4179 |
2004 | 9 | 4123 |
2004 | 12 | 4061 |
2005 | 3 | 4136 |
… | … | … |
2012 | 12 | 6424 |
2013 | 3 | 6300 |
2013 | 6 | 5905 |
2013 | 9 | 5618 |
2013 | 12 | 5702 |