pandas_ts

Submodules

Package Contents

Classes

TsAccessor

Accessor for operations on Series of TsDtype

TsDtype

Data type to handle packed time series data

class TsAccessor(series)[source]

Bases: collections.abc.MutableMapping

Accessor for operations on Series of TsDtype

This accessor implements MutableMapping interface over the fields of the struct, so you can access, change and delete the fields as if it was a dictionary, with [], [] = and del operators.

property flat_length: int

Length of the flat arrays

property fields: list[str]

Names of the nested columns

static _check_series(series)[source]
to_lists(fields: list[str] | None = None) pandas.DataFrame[source]

Convert ts into dataframe of list-array columns

Parameters:

fields (list[str] or None, optional) – Names of the fields to include. Default is None, which means all fields.

Returns:

Dataframe of list-arrays.

Return type:

pd.DataFrame

to_flat(fields: list[str] | None = None) pandas.DataFrame[source]

Convert ts into dataframe of flat arrays

Parameters:

fields (list[str] or None, optional) – Names of the fields to include. Default is None, which means all fields.

Returns:

Dataframe of flat arrays.

Return type:

pd.DataFrame

set_flat_field(field: str, value: numpy.typing.ArrayLike) None[source]

Set the field from flat-array of values, in-place

Parameters:
  • field (str) – Name of the field to set. If not present, it will be added.

  • value (ArrayLike) –

    Array of values to set. It must be a scalar or have the same length

    as the flat arrays, e.g. self.flat_length.

set_list_field(field: str, value: numpy.typing.ArrayLike) None[source]

Set the field from list-array, in-place

Parameters:
  • field (str) – Name of the field to set. If not present, it will be added.

  • value (ArrayLike) –

    Array of values to set. It must be a list-array of the same length

    as the series, e.g. length of the series.

pop_field(field: str) pandas.Series[source]

Delete the field from the struct and return it.

Parameters:

field (str) – Name of the field to delete.

Returns:

The deleted field.

Return type:

pd.Series

query_flat(query: str) pandas.Series[source]

Query the flat arrays with a boolean expression

Currently, it will remove empty rows from the output series. # TODO: preserve the index keeping empty rows

Parameters:

query (str) – Boolean expression to filter the rows.

Returns:

The filtered series.

Return type:

pd.Series

get_list_series(field: str) pandas.Series[source]

Get the list-array field as a Series

Parameters:

field (str) – Name of the field to get.

Returns:

The list-array field.

Return type:

pd.Series

__getitem__(key: str | list[str]) pandas.Series[source]
__setitem__(key: str, value: numpy.typing.ArrayLike) None[source]
__delitem__(key: str) None[source]
__iter__() collections.abc.Generator[str, None, None][source]
__len__() int[source]
class TsDtype(pyarrow_dtype: pyarrow.DataType)[source]

Bases: pandas.ArrowDtype

Data type to handle packed time series data

property name: str

The string representation of the ts type

pyarrow_dtype: pyarrow.StructType
type

The type of the array’s elements, always pd.DataFrame

classmethod from_fields(fields: collections.abc.Mapping[str, pyarrow.DataType]) Self[source]

Make TsDtype from a mapping of field names and list item types.

Parameters:

fields (Mapping[str, pa.DataType]) – A mapping of field names and their item types. Since all fields are lists, the item types are inner types of the lists, not the list types themselves.

Returns:

The constructed TsDtype.

Return type:

TsDtype

Examples

>>> dtype = TsDtype.from_fields({"a": pa.float64(), "b": pa.int64()})
>>> dtype
ts<a: [double], b: [int64]>
>>> assert (
...     dtype.pyarrow_dtype
...     == pa.struct({"a": pa.list_(pa.float64()), "b": pa.list_(pa.int64())})
... )
static _validate_dtype(pyarrow_dtype: pyarrow.DataType) pyarrow.StructType[source]
classmethod construct_from_string(string: str) Self[source]

Construct TsDtype from a string representation.

This works only for simple types, i.e. non-parametric pyarrow types.

Parameters:

string (str) – The string representation of the ts type. For example, ‘ts<x: [int64], y: [float64]’. It must be consistent with the string representation of the dtype given by the name attribute.

Returns:

The constructed TsDtype.

Return type:

TsDtype

Raises:

ValueError – If the string is not a valid ts type string or if the element types are parametric pyarrow types.

classmethod from_pandas_arrow_dtype(pandas_arrow_dtype: pandas.ArrowDtype)[source]

Construct TsDtype from a pandas.ArrowDtype.

Parameters:

pandas_arrow_dtype (ArrowDtype) – The pandas.ArrowDtype to construct TsDtype from.

Returns:

The constructed TsDtype.

Return type:

TsDtype

Raises:

ValueError – If the given dtype is not a valid ts type.

classmethod construct_array_type() type[pandas.core.arrays.ArrowExtensionArray][source]

Corresponded array type, always TsExtensionArray