Skip to content

Commit

Permalink
subdtype (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage authored Dec 29, 2024
1 parent 32626b1 commit 3445aab
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build/
dist/
MANIFEST
*pytest_cache*
*mypy_cache*
.eggs

# WebDAV file system cache files
Expand Down
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pint-pandas Changelog
--------------------

- Added `__array_function__` support for numpy fuctions like clip.
- `PintArray` now explictly shows its `subtype`, the data type of its magnitude array, eg `pint[m][Float64]`


0.6.2 (2024-07-29)
Expand Down
2 changes: 0 additions & 2 deletions docs/getting/tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. _tutorial:

**************************
Tutorial
**************************
Expand Down
3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:orphan:

Pint-pandas: Unit support for pandas
======================
=====================================

**Useful links**:
`Code Repository <https://github.com/hgrecco/pint-pandas>`__ |
Expand Down Expand Up @@ -66,7 +66,6 @@ Pint-pandas: Unit support for pandas

Getting started <getting/index>
User Guide <user/index>
Advanced topics <advanced/index>
ecosystem

.. toctree::
Expand Down
1 change: 1 addition & 0 deletions docs/user/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The default operation of Pandas `pd.concat` function is to perform row-wise conc
:suppress:
:okexcept:
list_of_series = [pd.Series([1.0, 2.0], dtype="pint[m]") for i in range(0, 10)]
df = pd.concat(list_of_series, axis=1)
Expand Down
35 changes: 25 additions & 10 deletions docs/user/initializing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,50 @@
Initializing data
**************************

There are several ways to initialize PintArrays in a DataFrame. Here's the most common methods. We'll use `PA_` and `Q_` as shorthand for PintArray and Quantity.
There are several ways to initialize a `PintArray`s` in a `DataFrame`. Here's the most common methods. We'll use `PA_` and `Q_` as shorthand for `PintArray` and `Quantity`.



.. ipython:: python
:okwarning:
import pandas as pd
import pint
import pint_pandas
import io
PA_ = pint_pandas.PintArray
ureg = pint_pandas.PintType.ureg
Q_ = ureg.Quantity
df = pd.DataFrame(
{
"A": pd.Series([1.0, 2.0], dtype="pint[m]"),
"B": pd.Series([1.0, 2.0]).astype("pint[m]"),
"C": PA_([2.0, 3.0], dtype="pint[m]"),
"D": PA_([2.0, 3.0], dtype="m"),
"E": PA_([2.0, 3.0], dtype=ureg.m),
"F": PA_.from_1darray_quantity(Q_([2, 3], ureg.m)),
"G": PA_(Q_([2.0, 3.0], ureg.m)),
"Ser1": pd.Series([1, 2], dtype="pint[m]"),
"Ser2": pd.Series([1, 2]).astype("pint[m]"),
"Ser3": pd.Series([1, 2], dtype="pint[m][Int64]"),
"Ser4": pd.Series([1, 2]).astype("pint[m][Int64]"),
"PArr1": PA_([1, 2], dtype="pint[m]"),
"PArr2": PA_([1, 2], dtype="pint[m][Int64]"),
"PArr3": PA_([1, 2], dtype="m"),
"PArr4": PA_([1, 2], dtype=ureg.m),
"PArr5": PA_(Q_([1, 2], ureg.m)),
"PArr6": PA_([1, 2],"m"),
}
)
df
In the first two Series examples above, the data was converted to Float64.

.. ipython:: python
df.dtypes
To avoid this conversion, specify the subdtype (dtype of the magnitudes) in the dtype `"pint[m][Int64]"` when constructing using a `Series`. The default data dtype that pint-pandas converts to can be changed by modifying `pint_pandas.DEFAULT_SUBDTYPE`.

`PintArray` infers the subdtype from the data passed into it when there is no subdtype specified in the dtype. It also accepts a pint `Unit`` or unit string as the dtype.


.. note::

"pint[unit]" must be used for the Series or DataFrame constuctor.
`"pint[unit]"` or `"pint[unit][subdtype]"` must be used for the Series or DataFrame constuctor.
Loading

0 comments on commit 3445aab

Please sign in to comment.