to_wide_matrix¶
to_wide_matrix
¶
to_wide_matrix(panel: DataFrame, *, value_col: str = 'num_bikes_available', time_col: str = 'fetched_at', station_col: str = 'station_id') -> DataFrame
Pivot a long panel into a time-by-station matrix.
The "long/tidy" panel is right for storage but most external tools (scikit-learn,
a correlation matrix, a heatmap) want the "wide" form: rows are timestamps, columns
are stations, cells are value_col. pivot_table does this but leaves fiddly
column-index names to clean up; this returns a flat, ready-to-use matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
panel
|
DataFrame
|
Long panel/frame with |
required |
value_col
|
str
|
The cell value, the row key and the column key. |
'num_bikes_available'
|
time_col
|
str
|
The cell value, the row key and the column key. |
'num_bikes_available'
|
station_col
|
str
|
The cell value, the row key and the column key. |
'num_bikes_available'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Indexed by |
See Also
extract_snapshot_asof : Take a single row of this matrix.
resample_panel : Put the rows on a regular grid first.
Examples:
>>> import pandas as pd
>>> panel = pd.DataFrame({
... "station_id": ["a", "b", "a", "b"],
... "fetched_at": pd.to_datetime(
... ["2026-01-01T08:00Z"] * 2 + ["2026-01-01T09:00Z"] * 2),
... "num_bikes_available": [5, 2, 3, 8],
... })
>>> to_wide_matrix(panel).shape
(2, 2)