cds2.data_analysis
Pandas-backed DataSet and DataFrame bridge.
Pandas-backed data analysis — DataSet and DataFrame bridge.
A :class:DataSet is the v2 successor to the pure-Python cds.data_analysis.DataSet
from v1. The public API is intentionally compatible (columns, shape,
column, filter, head/tail, select, group_by,
to_list) so existing tutorials port without rewrites, but the internal
storage is a :class:pandas.DataFrame for vectorised speed and for seamless
interoperability with the pandas ecosystem.
Design choices mirror the v1 pandas_io bridge:
Noneround-trips asNaNin numeric columns and back toNone.- Column order is preserved in both directions.
- The module can be used either through instance methods
(
ds.to_dataframe()/DataSet.from_dataframe(df)) or the free functions :func:to_dataframe/ :func:from_dataframe. - Summary statistics are exposed via :meth:
DataSet.describe(a thin wrapper around :meth:pandas.DataFrame.describe) and :meth:DataSet.summarize(column-level null / unique / numeric moments compatible with :func:cds2.io.summarize).
DataSet
A lightweight DataFrame-like container backed by :class:pandas.DataFrame.
Data is stored internally as a :class:pandas.DataFrame while the public
API stays compatible with the v1 pure-Python implementation (list-of-dicts
construction, column/filter/select/group_by helpers).
Source code in src/cds2/data_analysis.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
columns
property
Return the ordered list of column names.
shape
property
Return (rows, columns).
__init__(data=None)
Create a dataset.
Args:
data: either a list of row dictionaries (Row) or a
:class:pandas.DataFrame. None or an empty list creates
an empty dataset with no columns.
Source code in src/cds2/data_analysis.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
__len__()
Return the number of rows.
Source code in src/cds2/data_analysis.py
98 99 100 | |
__getitem__(idx)
Return the row at integer position idx as a :class:Row.
Source code in src/cds2/data_analysis.py
102 103 104 105 106 107 108 109 110 | |
column(name)
Extract a single column as a list, mapping NaN to None.
Source code in src/cds2/data_analysis.py
112 113 114 115 116 117 118 119 120 | |
filter(predicate)
Filter rows by a predicate applied to each :class:Row.
Source code in src/cds2/data_analysis.py
122 123 124 125 | |
head(n=5)
Return the first n rows.
Source code in src/cds2/data_analysis.py
127 128 129 | |
tail(n=5)
Return the last n rows.
Source code in src/cds2/data_analysis.py
131 132 133 | |
select(*names)
Select a subset of columns by name, preserving order.
Source code in src/cds2/data_analysis.py
135 136 137 138 139 140 141 | |
group_by(column_name)
Group rows by column_name for aggregation.
Source code in src/cds2/data_analysis.py
143 144 145 146 147 | |
to_list()
Export data as a list of dictionaries, mapping NaN to None.
Source code in src/cds2/data_analysis.py
149 150 151 152 153 154 | |
to_dataframe()
Return a copy of the underlying :class:pandas.DataFrame.
Source code in src/cds2/data_analysis.py
156 157 158 | |
from_dataframe(df)
classmethod
Create a :class:DataSet from a :class:pandas.DataFrame.
Source code in src/cds2/data_analysis.py
160 161 162 163 | |
describe()
Descriptive statistics via :meth:pandas.DataFrame.describe.
Includes all columns (numeric and non-numeric) when data is present;
returns an empty :class:DataFrame for an empty dataset.
Source code in src/cds2/data_analysis.py
165 166 167 168 169 170 171 172 173 174 | |
summarize()
Column-level summary: dtypes, nulls, uniques and numeric moments.
Mirrors :func:cds2.io.summarize but operates directly on a
:class:DataSet. Returns a :class:pandas.DataFrame with one row per
column and fields column, dtype, non_null, nulls,
unique, mean, std, min, max.
Source code in src/cds2/data_analysis.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
__repr__()
Return a compact summary.
Source code in src/cds2/data_analysis.py
237 238 239 240 241 | |
DataGroup
Helper for grouped aggregations, backed by pandas.groupby.
Source code in src/cds2/data_analysis.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
__init__(df, group_col)
Store the source frame and grouping column.
Source code in src/cds2/data_analysis.py
247 248 249 250 | |
mean(numeric_col)
Calculate the mean of numeric_col for each group.
Non-numeric cells are ignored; groups with no numeric values map to
0.0 (matching the v1 behaviour).
Source code in src/cds2/data_analysis.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
count()
Count the number of rows in each group.
Source code in src/cds2/data_analysis.py
278 279 280 281 282 283 284 285 | |
to_dataframe(ds)
Convert a :class:DataSet to a :class:pandas.DataFrame.
Thin wrapper around :meth:DataSet.to_dataframe for compatibility with
the v1 cds.data_analysis.pandas_io.to_dataframe bridge.
Source code in src/cds2/data_analysis.py
288 289 290 291 292 293 294 | |
from_dataframe(df)
Convert a :class:pandas.DataFrame to a :class:DataSet.
Thin wrapper around :meth:DataSet.from_dataframe for compatibility with
the v1 cds.data_analysis.pandas_io.from_dataframe bridge. NaN
values are exposed as None through the :class:DataSet API, matching
the v1 scalar contract.
Source code in src/cds2/data_analysis.py
297 298 299 300 301 302 303 304 305 | |