{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data types\n", "\n", "In most cases, the data type (`dtype`) of a [Variable](../generated/classes/scipp.Variable.rst) is derived from the data.\n", "For instance when passing a [numpy array](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) to scipp, scipp will use the `dtype` provided by numpy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipp as sc\n", "\n", "var = sc.Variable(dims=['x'], values=np.arange(4.0))\n", "var.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.Variable(dims=['x'], values=np.arange(4))\n", "var.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `dtype` may also be specified using a keyword argument to `sc.Variable` and most [creation functions](./api.rst#creation-functions).\n", "It is possible to use scipp's own `scipp.dtype`, [numpy.dtype](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html), or (where a numpy equivalent exists) a string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.zeros(dims=['x'], shape=[2], dtype=sc.dtype.float32)\n", "var.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.zeros(dims=['x'], shape=[2], dtype=np.dtype(np.float32))\n", "var.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.zeros(dims=['x'], shape=[2], dtype='float32')\n", "var.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scipp supports common dtypes like\n", "\n", "- `float32`, `float64`\n", "- `int32`, `int64`\n", "- `bool`\n", "- `string`\n", "- `datetime64`\n", "\n", "It is also possible to nest Variables, DataArrays, or Datasets inside of Variables. This is useful for storing attributes in DataArrays and Datasets. But there is only limited interoperability with numpy in those cases." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.scalar(sc.zeros(dims=['x'], shape=[2], dtype='float64'))\n", "var" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get a full list using \n", "```python\n", "[s for s in dir(sc.dtype) if not s.startswith('__')]\n", "```\n", "but note that many of those dtypes are only meant for internal use." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dates and Times\n", "\n", "Scipp has a special dtype for time-points, `sc.dtype.datetime64`.\n", "Variables can be constructed from integers which encode the time since the [Unix epoch](https://en.wikipedia.org/wiki/Unix_time):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar(value=0, unit=sc.units.s, dtype=sc.dtype.datetime64)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar(value=681794055, unit=sc.units.s, dtype=sc.dtype.datetime64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Datetime variables always need a temporal unit and that unit determines how the integer that is passed to `value=` is interpreted:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = sc.scalar(value=681794055, unit=sc.units.ns, dtype=sc.dtype.datetime64)\n", "var" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Datetime elements are automatically converted to and from [numpy.datetime64](https://numpy.org/doc/stable/reference/arrays.datetime.html#basic-datetimes) objects:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "now = sc.scalar(value=np.datetime64('now'))\n", "now" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that `now` has unit `s` even though we did not specify it.\n", "The unit was deduced from the `numpy.datetime64` object which encodes a unit of its own." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operations\n", "\n", "Variables containing datetimes only support a limited set of operations as it makes no sense to, for instance, add two time points.\n", "In contrast to numpy, scipp does not have a separate type for time differences.\n", "Those are simply encoded by integer Variables with a temporal unit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = sc.scalar(value=np.datetime64('2021-03-14', 'ms'))\n", "b = sc.scalar(value=np.datetime64('2000-01-01', 'ms'))\n", "a - b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " a + b\n", "except sc.DTypeError as err:\n", " print(err)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + sc.scalar(value=123, unit='ms')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Time zones\n", "\n", "Scipp does not support manual handling of time zones.\n", "All datetime objects are assumed to be in UTC.\n", "Scipp does not look at your local time zone, thus the following will always produce 12:30 on 2021-03-09 UTC no matter where you are when you run this code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.scalar(value=np.datetime64('2021-09-03T12:30:00'))" ] } ], "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }