DataFrame interop (pandas, polars)¶
Lazy-imported converters between TSeries / MVTSeries / Workspace and
pandas.DataFrame / polars.DataFrame. Neither library is a hard dependency;
install with the optional extra:
The pandas surface accepts index="auto" | "mit" | "date"; the polars surface
keeps the time axis as a column (polars has no row index) and requires explicit
freq= on from_polars(date_column=...) since polars carries no period semantics.
tsecon.interop.pandas¶
tsecon.interop.pandas ¶
pandas DataFrame interop (lazy / optional).
pandas is not a hard dependency: this module raises :class:ImportError
with an install hint when called without pandas installed.
Public API
- :func:
to_pandas—TSeries/MVTSeries/Workspace→pd.Series/pd.DataFrame. - :func:
from_pandas—pd.Series/pd.DataFrame→TSeries/MVTSeries/Workspace.
Required DataFrame shape (for :func:from_pandas)
The time axis must live on one of:
-
the row index, of one of the following types:
-
:class:
pd.PeriodIndex— frequency inferred from.freqstr; nofreq=kwarg needed. Supports pandas 2.x (Q-DEC,M, ...) and pandas 3.x (QE-DEC,ME,YE-DEC, ...) aliases. - :class:
pd.DatetimeIndex— frequency cannot be auto-inferred; thefreq=kwarg is required (one of :class:~tsecon.Daily, :class:~tsecon.BDaily, :class:~tsecon.Weekly, etc.). - :class:
pd.RangeIndexor any integer index — interpreted as :class:~tsecon.frequencies.Unitby default; passfreq=Unit()explicitly to be explicit. -
Object dtype of :class:
~tsecon.MITinstances — the frequency is read off the MITs themselves; all MITs must share a single frequency. This is the round-trip target of :func:to_pandas(..., index="mit"). -
a column named
time_col(must be passed explicitly). The same type / dtype rules apply, with a Date / Datetime column matching :class:pd.DatetimeIndex.
The time axis must be strictly ascending and contiguous (no gaps, no
duplicates). Sparse or unsorted inputs raise :class:ValueError; callers
who need gap-filling should sort_index().asfreq(...) on the pandas
side first.
For wide DataFrames (default, wide=True), every non-time column
becomes a variable. For long DataFrames (wide=False), time_col,
name_col, and value_col are all required; the frame is pivoted
into wide form before conversion.
Return type
- :class:
~tsecon.TSerieswhen the input is a Series. - :class:
~tsecon.Workspacewhento_workspace=True(one named TSeries per non-time column, in DataFrame column order; per-column dtypes preserved). - :class:
~tsecon.MVTSeriesotherwise (one column per variable, single shared dtype, numeric-only).
Output shape (for :func:to_pandas)
The index argument controls how the time axis is materialised:
"auto"— :class:pd.PeriodIndexfor Yearly / Quarterly / Monthly / Weekly, :class:pd.DatetimeIndex(period-end dates) for Daily / BDaily, :class:pd.RangeIndexfor Unit, and an object-dtype Index of MIT for HalfYearly (which pandas has no period analogue for)."mit"— always emit an object-dtype Index of MIT instances. The only fully lossless option (re-from_pandasround-trips exactly)."date"— always emit a :class:pd.DatetimeIndexof period-end dates (or period-begin dates ifdate_ref="begin"). Lossy for non-Daily frequencies on round-trip unlessfreq=is passed back to :func:from_pandas.
to_pandas ¶
to_pandas(
obj: TSeries | MVTSeries | Workspace,
*,
index: IndexKind = "auto",
date_ref: Literal["begin", "end"] = "end",
name: str | None = None,
) -> pd.Series | pd.DataFrame
Convert a tsecon container to a pandas Series or DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
TSeries | MVTSeries | Workspace
|
A :class: |
required |
index
|
IndexKind
|
Time-axis representation. See module docstring. |
'auto'
|
date_ref
|
Literal['begin', 'end']
|
Period-anchor when |
'end'
|
name
|
str | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
Series or DataFrame
|
A Series for TSeries; a DataFrame otherwise. |
Source code in src/tsecon/interop/pandas.py
from_pandas ¶
from_pandas(
obj: Series | DataFrame,
*,
freq: Frequency | None = None,
wide: bool = True,
time_col: str | None = None,
name_col: str | None = None,
value_col: str | None = None,
to_workspace: bool = False,
) -> TSeries | MVTSeries | Workspace
Convert a pandas Series / DataFrame to a tsecon container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Series | DataFrame
|
A |
required |
freq
|
Frequency | None
|
Required when the time axis is a :class: |
None
|
wide
|
bool
|
When |
True
|
time_col
|
str | None
|
Optional name of the column to use as the time axis. When unset, the DataFrame index is used. |
None
|
name_col
|
str | None
|
Long-format only. Name of the column holding the per-row variable
name. Required when |
None
|
value_col
|
str | None
|
Long-format only. Name of the column holding the per-row value.
Required when |
None
|
to_workspace
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
TSeries or MVTSeries or Workspace
|
A TSeries when the input is a Series; a Workspace when
|
Source code in src/tsecon/interop/pandas.py
tsecon.interop.polars¶
tsecon.interop.polars ¶
polars DataFrame interop (lazy / optional).
polars is not a hard dependency: this module raises :class:ImportError
with an install hint when called without polars installed.
polars has no row-index concept, so the time axis is always a regular
column. The default name is "time" (override with time_col=).
Public API
- :func:
to_polars—TSeries/MVTSeries/Workspace→ :class:polars.DataFrame. - :func:
from_polars— :class:polars.DataFrame→TSeries/MVTSeries/Workspace.
Required DataFrame shape (for :func:from_polars)
The DataFrame must contain a column named time_col (default
"time") of one of the following dtypes:
- :class:
polars.Dateor :class:polars.Datetime— thefreq=kwarg is required; polars carries no period semantics so the frequency cannot be auto-inferred. Pass a :class:~tsecon.Daily/ :class:~tsecon.BDaily/ :class:~tsecon.Weekly/ :class:~tsecon.Monthly/ :class:~tsecon.Quarterly/ :class:~tsecon.HalfYearly/ :class:~tsecon.Yearlyinstance. - Any integer dtype — interpreted as :class:
~tsecon.frequencies.Unitby default; passfreq=Unit()explicitly to be explicit. The column values are taken as the rawMIT.valueintegers.
The time column must be strictly ascending and contiguous (no gaps,
no duplicates). Sparse or unsorted input raises :class:ValueError;
callers who need gap-filling should pre-process with polars before
calling :func:from_polars.
For wide DataFrames (default, wide=True), every non-time column
becomes a variable. For long DataFrames (wide=False), time_col,
name_col, and value_col are all required; the frame is pivoted
into wide form before conversion.
Return type
- :class:
~tsecon.Workspacewhento_workspace=True(one named TSeries per non-time column, in DataFrame column order; per-column dtypes preserved). - :class:
~tsecon.TSerieswhen the wide form has a single non-time column (andto_workspace=False). - :class:
~tsecon.MVTSeriesotherwise.
Output shape (for :func:to_polars)
The time column dtype is :class:polars.Date for Daily / BDaily / Weekly
and YP frequencies (using period-end dates by default; date_ref="begin"
swaps to period-begin), and :class:polars.Int64 for Unit (raw MIT
values).
to_polars ¶
to_polars(
obj: TSeries | MVTSeries | Workspace,
*,
time_col: str = "time",
date_ref: Literal["begin", "end"] = "end",
value_col: str = "value",
) -> Any
Convert a tsecon container to a polars DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
TSeries | MVTSeries | Workspace
|
A :class: |
required |
time_col
|
str
|
Name of the emitted time column. Default |
'time'
|
date_ref
|
Literal['begin', 'end']
|
Period-anchor for non-Unit frequencies. |
'end'
|
value_col
|
str
|
Name of the value column when converting a TSeries (ignored for
MVTSeries / Workspace). Default |
'value'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Annotated as |
Source code in src/tsecon/interop/polars.py
from_polars ¶
from_polars(
obj: DataFrame,
*,
freq: Frequency | None = None,
wide: bool = True,
time_col: str = "time",
name_col: str | None = None,
value_col: str | None = None,
to_workspace: bool = False,
) -> TSeries | MVTSeries | Workspace
Convert a polars DataFrame to a tsecon container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
DataFrame
|
A :class: |
required |
freq
|
Frequency | None
|
Required when the time column dtype is :class: |
None
|
wide
|
bool
|
When |
True
|
time_col
|
str
|
Name of the time column. Default |
'time'
|
name_col
|
str | None
|
Long-format only. |
None
|
value_col
|
str | None
|
Long-format only. Also used to decide the resulting type: a single-value-column wide DataFrame returns a TSeries; otherwise an MVTSeries is returned. |
None
|
to_workspace
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
TSeries or MVTSeries or Workspace
|
A Workspace when |