Pandas
symconstraints.pandas
Integration with Pandas to aid in data cleaning dataframes.
symbols(df, symbol_list, **kwargs)
Return SymPy symbols with assumptions inferred from the dataframe dtypes.
Currently, it infers the following:
- Unsigned integer dtypes are inferred to be nonnegative integers
- Float dtypes are inferred to be real numbers
- Complex dtypes are inferred to be complex numbers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Dataframe to infer symbol assumptions from. |
required |
symbol_list |
str | list[str]
|
List of columns, can be represented as a space separated string. |
required |
**kwargs |
Extra arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Symbol | list[Symbol]
|
SymPy symbols with assumptions inferred from the dataframe dtypes, corresponding to each column given. Returns a list if multiple columns are given, or a single symbol if a signle column is given. |
Raises:
Type | Description |
---|---|
ValueError
|
Raises a ValueError if a column is not found or an unsupported dtype is found. |
Examples:
>>> import pandas as pd
>>> from symconstraints.pandas import symbols
>>> df = pd.DataFrame({
... 'Level': [1],
... 'Width': [5.3],
... 'Height': [7.6],
... 'Voltage': [5+3j]
... }).astype({'Level': 'uint8'})
>>> level, width, voltage = symbols(df, 'Level Width Voltage')
>>> level.is_nonnegative, level.is_integer
(True, True)
>>> width.is_integer, width.is_real
(None, True)
>>> voltage.is_real, voltage.is_complex
(None, True)
Source code in src/symconstraints/pandas.py
check(constraints, df)
Return a table checking all the validations provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints |
Constraints | Validation
|
|
required |
df |
DataFrame
|
Dataframe to check |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Returns a dataframe showing the result of all the validations for each row in the dataframe.
Each column in the dataframe corresponds to each validation operation provided. Each row
corresponds to each row in the original dataframe. A result of 1 is shown for successful validation,
0 for an unsuccessful validation, and NaN for validations that can't be computed due to missing values.
The result dtype is If a |
Examples:
>>> import pandas as pd
>>> from symconstraints.pandas import symbols, check
>>> from symconstraints import Constraints
>>> from sympy import Eq
>>> df = pd.DataFrame({
... 'A': [5,6,8,9],
... 'B': [3,5,90,None],
... 'C': [14, 30, None, None]
... }, dtype=float)
>>> A, B, C = symbols(df, ['A', 'B', 'C'])
>>> constraints = Constraints([A > B, Eq(C, B*A)])
>>> check(constraints, df)
# Order may differ
(A, B) (C, A, B) (C, B) (C, A)
A > B Eq(C, A*B) B < C/B A > C/A
0 1.0 0.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 0.0 NaN NaN NaN
3 NaN NaN NaN NaN
Source code in src/symconstraints/pandas.py
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 |
|
set_invalid_all(check_result, df, fill=math.nan)
Replace all possible invalid values in the dataframe to a set value.
This replaces values in the dataframe that could possibly be invalid under the given constraints. This might help get rid of outlier data within the dataframe.
The input dataframe is copied and is not edited in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
check_result |
DataFrame
|
Check result returned by |
required |
df |
DataFrame
|
Dataframe to edit |
required |
fill |
Any
|
The set value to replace invalid values. |
nan
|
Returns:
Type | Description |
---|---|
DataFrame
|
Dataframe with replaced values. |
Examples:
>>> import pandas as pd
>>> from symconstraints import Constraints
>>> from symconstraints.pandas import symbols, check, set_invalid_all
>>> from sympy import Eq
>>> df = pd.DataFrame(
... {
... "height": [5, 6, 8, 9],
... "width": [3, 5, 90, None],
... "area": [14, 30, None, 18],
... },
... dtype=float,
... )
>>> height, width, area = symbols(df, ["height", "width", "area"])
>>> constraints = Constraints([height > width, Eq(area, width * height)])
>>> check_result = check(constraints, df)
>>> set_invalid_all(check_result, df)
height width area
0 NaN NaN NaN
1 6.0 5.0 30.0
2 NaN NaN NaN
3 9.0 NaN 18.0
Source code in src/symconstraints/pandas.py
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 242 243 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 286 287 288 289 290 291 292 293 |
|
set_invalid_min(check_result, df, fill=math.nan, priority=None)
Replace the minimum amount of possible invalid values in the dataframe to a set value.
Similar to set_invalid_all
, this replaces values in the dataframe that could possibly be invalid under the given constraints.
However it does not replace all the values, instead it tries to replace the minimum amount of values in each row such that
it satisfies all the constraints. This might help get rid of outlier data within the dataframe, while also keeping more of the values
in the dataset unchanged.
The input dataframe is copied and is not edited in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
check_result |
DataFrame
|
Check result returned by |
required |
df |
DataFrame
|
Dataframe to edit |
required |
fill |
Any
|
The set value to replace invalid values. |
nan
|
priority |
list[Symbol] | None
|
In case of a tie in the decision to set which column, refer to this priority to decide which column to to set first. Priority is ordered from most preferred to be set to least preferred. |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
Dataframe with replaced values. |
Examples:
>>> import pandas as pd
>>> from symconstraints import Constraints
>>> from symconstraints.pandas import symbols, check, set_invalid_min
>>> from sympy import Eq
>>> df = pd.DataFrame(
... {
... "height": [5, 6, 8, 9, 7],
... "width": [3, 5, 90, None, 8],
... "depth": [None, 2, 10, 5, 5],
... "area": [14, 30, 10, None, 35],
... "volume": [None, 60, 100, 30, None],
... },
... dtype=float,
... )
>>> height, width, area, depth, volume = symbols(df, ["height", "width", "area", "depth", "volume"])
>>> constraints = Constraints([
... Eq(area, height * width),
... Eq(volume, area * depth),
... height > width,
... width > depth,
... ])
>>> check_result = check(constraints, df)
>>> set_invalid_min(check_result, df, priority=[volume, area, depth, width, height])
height width depth area volume
0 5.0 3.0 NaN NaN NaN
1 6.0 5.0 2.0 30.0 60.0
2 NaN 90.0 10.0 NaN 100.0
3 9.0 NaN 5.0 NaN 30.0
4 7.0 NaN 5.0 NaN NaN
Source code in src/symconstraints/pandas.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
|
impute(constraints, df)
Impute the dataframe under the given constraints.
This returns a copy of the dataframe with all NA values replaced with values inferred from
imputation operations given in the constraints. This assumes that all the values are valid,
so it is recommended that the dataframe be checked and that all of its invalid values are
removed via check
and set_invalid_all
.
The input dataframe is not edited in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints |
Constraints | Imputation
|
|
required |
df |
DataFrame
|
Dataframe to impute. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Imputed dataframe. |
Examples:
>>> import pandas as pd
>>> from symconstraints import Constraints
>>> from symconstraints.pandas import symbols, check, set_invalid_all, impute
>>> from sympy import Eq
>>> df = pd.DataFrame(
... {
... "height": [5, 6, 8, 9],
... "width": [3, 5, 7, None],
... "area": [14, 30, None, 18],
... },
... dtype=float,
... )
>>> height, width, area = symbols(df, ["height", "width", "area"])
>>> constraints = Constraints([height > width, Eq(area, width * height)])
>>> check_result = check(constraints, df)
>>> df = set_invalid_all(check_result, df)
>>> df
height width area
0 NaN NaN NaN
1 6.0 5.0 30.0
2 8.0 7.0 NaN
3 9.0 NaN 18.0
>>> imputed_df = impute(constraints, df)
>>> imputed_df
height width area
0 NaN NaN NaN
1 6.0 5.0 30.0
2 8.0 7.0 56.0
3 9.0 2.0 18.0
Source code in src/symconstraints/pandas.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|