Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ in pandas.
Construct a date :class:`~pandas.Series` with strings in ``YYYY-MM-DD`` format
or :class:`datetime.date` objects.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_date_create]
:end-before: [END bigquery_pandas_date_create]
.. code-block:: python

import datetime
import pandas as pd
import db_dtypes # noqa import to register dtypes

dates = pd.Series([datetime.date(2021, 9, 17), "2021-9-18"], dtype="dbdate")

Working with dates
^^^^^^^^^^^^^^^^^^
Expand All @@ -20,29 +22,26 @@ Convert a date :class:`~pandas.Series` to a ``datetime64`` Series with
:meth:`~pandas.Series.astype`. The resulting values use midnight as the
time part.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_date_as_datetime]
:end-before: [END bigquery_pandas_date_as_datetime]
.. code-block:: python

datetimes = dates.astype("datetime64")

Just like ``datetime64`` values, date values can be subtracted. This is
equivalent to first converting to ``datetime64`` and then subtracting.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_date_sub]
:end-before: [END bigquery_pandas_date_sub]
.. code-block:: python

dates2 = pd.Series(["2021-1-1", "2021-1-2"], dtype="dbdate")
diffs = dates - dates2

Just like ``datetime64`` values, :class:`~pandas.tseries.offsets.DateOffset`
values can be added to them.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_date_add_offset]
:end-before: [END bigquery_pandas_date_add_offset]
.. code-block:: python

do = pd.DateOffset(days=1)
after = dates + do
before = dates - do


Working with times
Expand All @@ -51,20 +50,16 @@ Working with times
Construct a time :class:`~pandas.Series` with strings in ``HH:MM:SS.fraction``
24-hour format or :class:`datetime.time` objects.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_time_create]
:end-before: [END bigquery_pandas_time_create]
.. code-block:: python

times = pd.Series([datetime.time(1, 2, 3, 456789), "12:00:00.6"], dtype="dbtime")

Convert a time :class:`~pandas.Series` to a ``timedelta64`` Series with
:meth:`~pandas.Series.astype`.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_time_as_timedelta]
:end-before: [END bigquery_pandas_time_as_timedelta]
.. code-block:: python

timedeltas = times.astype("timedelta64")


Combining dates and times
Expand All @@ -73,8 +68,6 @@ Combining dates and times
Combine a date :class:`~pandas.Series` with a time :class:`~pandas.Series` to
create a ``datetime64`` :class:`~pandas.Series`.

.. literalinclude:: samples/snippets/pandas_date_and_time.py
:language: python
:dedent: 4
:start-after: [START bigquery_pandas_combine_date_time]
:end-before: [END bigquery_pandas_combine_date_time]
.. code-block:: python

combined = dates + times
13 changes: 0 additions & 13 deletions samples/__init__.py

This file was deleted.

Empty file removed samples/pytest.ini
Empty file.
13 changes: 0 additions & 13 deletions samples/snippets/__init__.py

This file was deleted.

Loading
Loading