{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy date utilites\n", "===================\n", "\n", "This library provides utility functions to perform conversions and get\n", "information about numpy dates quickly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datetime\n", "import random\n", "import numpy\n", "import pyinterp\n", "\n", "def make_date(samples=10000):\n", " \"\"\"Generates random dates.\"\"\"\n", " epoch = datetime.datetime(1970, 1, 1)\n", " delta = datetime.datetime.now() - datetime.datetime(1970, 1, 1)\n", "\n", " pydates = [epoch + random.random() * delta for _ in range(samples)]\n", " npdates = numpy.array(pydates).astype(\"datetime64[ns]\")\n", "\n", " return npdates" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dates = make_date()\n", "dates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the date part as a structured numpy array of three fields: `year`,\n", "`month` and `day`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.date(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the time part as a structured numpy array of three fields: `hour`,\n", "`minute` and `second`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.time(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the ISO calendar of the date as a structured numpy array of three\n", "fields: `year`, `weekday` and `week`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.isocalendar(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the week day of the dates (Sunday is 0 ... Saturday is 6):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.weekday(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the timedelta from since january" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.timedelta_since_january(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the dates as datetime.datetime array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pyinterp.dateutils.datetime(dates)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 1 }