/
container.py
1443 lines (1244 loc) · 49.1 KB
/
container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
434
435
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Core pandas dataframe container specification."""
from __future__ import annotations
import copy
import os
import warnings
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, cast, overload
import pandas as pd
from pandera import errors
from pandera.config import CONFIG
from pandera import strategies as st
from pandera.api.base.schema import BaseSchema, inferred_schema_guard
from pandera.api.checks import Check
from pandera.api.hypotheses import Hypothesis
from pandera.api.pandas.types import (
CheckList,
PandasDtypeInputTypes,
StrictType,
)
from pandera.dtypes import DataType, UniqueSettings
from pandera.engines import pandas_engine, PYDANTIC_V2
if PYDANTIC_V2:
from pydantic_core import core_schema
from pydantic import GetCoreSchemaHandler
N_INDENT_SPACES = 4
class DataFrameSchema(
BaseSchema
): # pylint: disable=too-many-public-methods,too-many-locals
"""A light-weight pandas DataFrame validator."""
def __init__(
self,
columns: Optional[ # type: ignore [name-defined]
Dict[Any, "pandera.api.pandas.components.Column"] # type: ignore [name-defined]
] = None,
checks: Optional[CheckList] = None,
index=None,
dtype: PandasDtypeInputTypes = None,
coerce: bool = False,
strict: StrictType = False,
name: Optional[str] = None,
ordered: bool = False,
unique: Optional[Union[str, List[str]]] = None,
report_duplicates: UniqueSettings = "all",
unique_column_names: bool = False,
add_missing_columns: bool = False,
title: Optional[str] = None,
description: Optional[str] = None,
metadata: Optional[dict] = None,
drop_invalid_rows: bool = False,
) -> None:
"""Initialize DataFrameSchema validator.
:param columns: a dict where keys are column names and values are
Column objects specifying the datatypes and properties of a
particular column.
:type columns: mapping of column names and column schema component.
:param checks: dataframe-wide checks.
:param index: specify the datatypes and properties of the index.
:param dtype: datatype of the dataframe. This overrides the data
types specified in any of the columns. If a string is specified,
then assumes one of the valid pandas string values:
http://pandas.pydata.org/pandas-docs/stable/basics.html#dtypes.
:param coerce: whether or not to coerce all of the columns on
validation. This overrides any coerce setting at the column
or index level. This has no effect on columns where
``dtype=None``.
:param strict: ensure that all and only the columns defined in the
schema are present in the dataframe. If set to 'filter',
only the columns in the schema will be passed to the validated
dataframe. If set to filter and columns defined in the schema
are not present in the dataframe, will throw an error.
:param name: name of the schema.
:param ordered: whether or not to validate the columns order.
:param unique: a list of columns that should be jointly unique.
:param report_duplicates: how to report unique errors
- `exclude_first`: report all duplicates except first occurence
- `exclude_last`: report all duplicates except last occurence
- `all`: (default) report all duplicates
:param unique_column_names: whether or not column names must be unique.
:param add_missing_columns: add missing column names with either default
value, if specified in column schema, or NaN if column is nullable.
:param title: A human-readable label for the schema.
:param description: An arbitrary textual description of the schema.
:param metadata: An optional key-value data.
:param drop_invalid_rows: if True, drop invalid rows on validation.
:raises SchemaInitError: if impossible to build schema from parameters
:examples:
>>> import pandera as pa
>>>
>>> schema = pa.DataFrameSchema({
... "str_column": pa.Column(str),
... "float_column": pa.Column(float),
... "int_column": pa.Column(int),
... "date_column": pa.Column(pa.DateTime),
... })
Use the pandas API to define checks, which takes a function with
the signature: ``pd.Series -> Union[bool, pd.Series]`` where the
output series contains boolean values.
>>> schema_withchecks = pa.DataFrameSchema({
... "probability": pa.Column(
... float, pa.Check(lambda s: (s >= 0) & (s <= 1))),
...
... # check that the "category" column contains a few discrete
... # values, and the majority of the entries are dogs.
... "category": pa.Column(
... str, [
... pa.Check(lambda s: s.isin(["dog", "cat", "duck"])),
... pa.Check(lambda s: (s == "dog").mean() > 0.5),
... ]),
... })
See :ref:`here<DataFrameSchemas>` for more usage details.
"""
if columns is None:
columns = {}
_validate_columns(columns)
columns = _columns_renamed(columns)
if checks is None:
checks = []
if isinstance(checks, (Check, Hypothesis)):
checks = [checks]
super().__init__(
dtype=dtype,
checks=checks,
name=name,
title=title,
description=description,
metadata=metadata,
)
self.columns: Dict[Any, "pandera.api.pandas.components.Column"] = ( # type: ignore [name-defined]
{} if columns is None else columns
)
if strict not in (
False,
True,
"filter",
):
raise errors.SchemaInitError(
"strict parameter must equal either `True`, `False`, "
"or `'filter'`."
)
self.index = index
self.strict: Union[bool, str] = strict
self._coerce = coerce
self.ordered = ordered
self._unique = unique
self.report_duplicates = report_duplicates
self.unique_column_names = unique_column_names
self.add_missing_columns = add_missing_columns
self.drop_invalid_rows = drop_invalid_rows
# this attribute is not meant to be accessed by users and is explicitly
# set to True in the case that a schema is created by infer_schema.
self._IS_INFERRED = False
self.metadata = metadata
@property
def coerce(self) -> bool:
"""Whether to coerce series to specified type."""
if isinstance(self.dtype, DataType):
return self.dtype.auto_coerce or self._coerce
return self._coerce
@coerce.setter
def coerce(self, value: bool) -> None:
"""Set coerce attribute"""
self._coerce = value
@property
def unique(self):
"""List of columns that should be jointly unique."""
return self._unique
@unique.setter
def unique(self, value: Optional[Union[str, List[str]]]) -> None:
"""Set unique attribute."""
self._unique = [value] if isinstance(value, str) else value
# the _is_inferred getter and setter methods are not public
@property
def _is_inferred(self) -> bool:
return self._IS_INFERRED
@_is_inferred.setter
def _is_inferred(self, value: bool) -> None:
self._IS_INFERRED = value
@property
def dtypes(self) -> Dict[str, DataType]:
# pylint:disable=anomalous-backslash-in-string
"""
A dict where the keys are column names and values are
:class:`~pandera.dtypes.DataType` s for the column. Excludes columns
where `regex=True`.
:returns: dictionary of columns and their associated dtypes.
"""
regex_columns = [
name for name, col in self.columns.items() if col.regex
]
if regex_columns:
warnings.warn(
"Schema has columns specified as regex column names: "
f"{regex_columns}. Use the `get_dtypes` to get the datatypes "
"for these columns.",
UserWarning,
)
return {n: c.dtype for n, c in self.columns.items() if not c.regex}
def get_metadata(self) -> Optional[dict]:
"""Provide metadata for columns and schema level"""
res: Dict[Any, Any] = {"columns": {}}
for k in self.columns.keys():
res["columns"][k] = self.columns[k].properties["metadata"]
res["dataframe"] = self.metadata
meta = {}
meta[self.name] = res
return meta
def get_dtypes(self, dataframe: pd.DataFrame) -> Dict[str, DataType]:
"""
Same as the ``dtype`` property, but expands columns where
``regex == True`` based on the supplied dataframe.
:returns: dictionary of columns and their associated dtypes.
"""
regex_dtype = {}
for _, column in self.columns.items():
if column.regex:
regex_dtype.update(
{
c: column.dtype
for c in column.get_backend(
dataframe
).get_regex_columns(
column,
dataframe.columns,
)
}
)
return {
**{n: c.dtype for n, c in self.columns.items() if not c.regex},
**regex_dtype,
}
@property
def dtype(
self,
) -> DataType:
"""Get the dtype property."""
return self._dtype # type: ignore
@dtype.setter
def dtype(self, value: PandasDtypeInputTypes) -> None:
"""Set the pandas dtype property."""
self._dtype = pandas_engine.Engine.dtype(value) if value else None
def coerce_dtype(self, check_obj: pd.DataFrame) -> pd.DataFrame:
return self.get_backend(check_obj).coerce_dtype(check_obj, schema=self)
def validate(
self,
check_obj: pd.DataFrame,
head: Optional[int] = None,
tail: Optional[int] = None,
sample: Optional[int] = None,
random_state: Optional[int] = None,
lazy: bool = False,
inplace: bool = False,
) -> pd.DataFrame:
"""Check if all columns in a dataframe have a column in the Schema.
:param pd.DataFrame check_obj: the dataframe to be validated.
:param head: validate the first n rows. Rows overlapping with `tail` or
`sample` are de-duplicated.
:param tail: validate the last n rows. Rows overlapping with `head` or
`sample` are de-duplicated.
:param sample: validate a random sample of n rows. Rows overlapping
with `head` or `tail` are de-duplicated.
:param random_state: random seed for the ``sample`` argument.
:param lazy: if True, lazily evaluates dataframe against all validation
checks and raises a ``SchemaErrors``. Otherwise, raise
``SchemaError`` as soon as one occurs.
:param inplace: if True, applies coercion to the object of validation,
otherwise creates a copy of the data.
:returns: validated ``DataFrame``
:raises SchemaError: when ``DataFrame`` violates built-in or custom
checks.
:example:
Calling ``schema.validate`` returns the dataframe.
>>> import pandas as pd
>>> import pandera as pa
>>>
>>> df = pd.DataFrame({
... "probability": [0.1, 0.4, 0.52, 0.23, 0.8, 0.76],
... "category": ["dog", "dog", "cat", "duck", "dog", "dog"]
... })
>>>
>>> schema_withchecks = pa.DataFrameSchema({
... "probability": pa.Column(
... float, pa.Check(lambda s: (s >= 0) & (s <= 1))),
...
... # check that the "category" column contains a few discrete
... # values, and the majority of the entries are dogs.
... "category": pa.Column(
... str, [
... pa.Check(lambda s: s.isin(["dog", "cat", "duck"])),
... pa.Check(lambda s: (s == "dog").mean() > 0.5),
... ]),
... })
>>>
>>> schema_withchecks.validate(df)[["probability", "category"]]
probability category
0 0.10 dog
1 0.40 dog
2 0.52 cat
3 0.23 duck
4 0.80 dog
5 0.76 dog
"""
if not CONFIG.validation_enabled:
return check_obj
# NOTE: Move this into its own schema-backend variant. This is where
# the benefits of separating the schema spec from the backend
# implementation comes in.
if hasattr(check_obj, "dask"):
# special case for dask dataframes
if inplace:
check_obj = check_obj.pandera.add_schema(self)
else:
check_obj = check_obj.copy()
check_obj = check_obj.map_partitions( # type: ignore [operator]
self._validate,
head=head,
tail=tail,
sample=sample,
random_state=random_state,
lazy=lazy,
inplace=inplace,
meta=check_obj,
)
return check_obj.pandera.add_schema(self)
return self._validate(
check_obj=check_obj,
head=head,
tail=tail,
sample=sample,
random_state=random_state,
lazy=lazy,
inplace=inplace,
)
def _validate(
self,
check_obj: pd.DataFrame,
head: Optional[int] = None,
tail: Optional[int] = None,
sample: Optional[int] = None,
random_state: Optional[int] = None,
lazy: bool = False,
inplace: bool = False,
) -> pd.DataFrame:
if self._is_inferred:
warnings.warn(
f"This {type(self)} is an inferred schema that hasn't been "
"modified. It's recommended that you refine the schema "
"by calling `add_columns`, `remove_columns`, or "
"`update_columns` before using it to validate data.",
UserWarning,
)
return self.get_backend(check_obj).validate(
check_obj,
schema=self,
head=head,
tail=tail,
sample=sample,
random_state=random_state,
lazy=lazy,
inplace=inplace,
)
def __call__(
self,
dataframe: pd.DataFrame,
head: Optional[int] = None,
tail: Optional[int] = None,
sample: Optional[int] = None,
random_state: Optional[int] = None,
lazy: bool = False,
inplace: bool = False,
):
"""Alias for :func:`DataFrameSchema.validate` method.
:param pd.DataFrame dataframe: the dataframe to be validated.
:param head: validate the first n rows. Rows overlapping with `tail` or
`sample` are de-duplicated.
:type head: int
:param tail: validate the last n rows. Rows overlapping with `head` or
`sample` are de-duplicated.
:type tail: int
:param sample: validate a random sample of n rows. Rows overlapping
with `head` or `tail` are de-duplicated.
:param random_state: random seed for the ``sample`` argument.
:param lazy: if True, lazily evaluates dataframe against all validation
checks and raises a ``SchemaErrors``. Otherwise, raise
``SchemaError`` as soon as one occurs.
:param inplace: if True, applies coercion to the object of validation,
otherwise creates a copy of the data.
"""
return self.validate(
dataframe, head, tail, sample, random_state, lazy, inplace
)
def __repr__(self) -> str:
"""Represent string for logging."""
return (
f"<Schema {self.__class__.__name__}("
f"columns={self.columns}, "
f"checks={self.checks}, "
f"index={self.index.__repr__()}, "
f"coerce={self.coerce}, "
f"dtype={self._dtype}, "
f"strict={self.strict}, "
f"name={self.name}, "
f"ordered={self.ordered}, "
f"unique_column_names={self.unique_column_names}"
f"metadata='{self.metadata}, "
f"unique_column_names={self.unique_column_names}, "
f"add_missing_columns={self.add_missing_columns}"
")>"
)
def __str__(self) -> str:
"""Represent string for user inspection."""
def _format_multiline(json_str, arg):
return "\n".join(
f"{indent}{line}" if i != 0 else f"{indent}{arg}={line}"
for i, line in enumerate(json_str.split("\n"))
)
indent = " " * N_INDENT_SPACES
if self.columns:
columns_str = f"{indent}columns={{\n"
for colname, col in self.columns.items():
columns_str += f"{indent * 2}'{colname}': {col}\n"
columns_str += f"{indent}}}"
else:
columns_str = f"{indent}columns={{}}"
if self.checks:
checks_str = f"{indent}checks=[\n"
for check in self.checks:
checks_str += f"{indent * 2}{check}\n"
checks_str += f"{indent}]"
else:
checks_str = f"{indent}checks=[]"
# add additional indents
index_ = str(self.index).split("\n")
if len(index_) == 1:
index = str(self.index)
else:
index = "\n".join(
x if i == 0 else f"{indent}{x}" for i, x in enumerate(index_)
)
return (
f"<Schema {self.__class__.__name__}(\n"
f"{columns_str},\n"
f"{checks_str},\n"
f"{indent}coerce={self.coerce},\n"
f"{indent}dtype={self._dtype},\n"
f"{indent}index={index},\n"
f"{indent}strict={self.strict},\n"
f"{indent}name={self.name},\n"
f"{indent}ordered={self.ordered},\n"
f"{indent}unique_column_names={self.unique_column_names},\n"
f"{indent}metadata={self.metadata}, \n"
f"{indent}add_missing_columns={self.add_missing_columns}\n"
")>"
)
def __eq__(self, other: object) -> bool:
if not isinstance(other, type(self)):
return NotImplemented
def _compare_dict(obj):
return {
k: v for k, v in obj.__dict__.items() if k != "_IS_INFERRED"
}
return _compare_dict(self) == _compare_dict(other)
if PYDANTIC_V2:
@classmethod
def __get_pydantic_core_schema__(
cls, _source_type: Any, _handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.no_info_plain_validator_function(
cls._pydantic_validate,
)
else:
@classmethod
def __get_validators__(cls):
yield cls._pydantic_validate
@classmethod
def _pydantic_validate(cls, schema: Any) -> "DataFrameSchema":
"""Verify that the input is a compatible DataFrameSchema."""
if not isinstance(schema, cls): # type: ignore
raise TypeError(f"{schema} is not a {cls}.")
return cast("DataFrameSchema", schema)
#################################
# Schema Transformation Methods #
#################################
@inferred_schema_guard
def add_columns(
self, extra_schema_cols: Dict[str, Any]
) -> "DataFrameSchema":
"""Create a copy of the :class:`DataFrameSchema` with extra columns.
:param extra_schema_cols: Additional columns of the format
:type extra_schema_cols: DataFrameSchema
:returns: a new :class:`DataFrameSchema` with the extra_schema_cols
added.
:example:
To add columns to the schema, pass a dictionary with column name and
``Column`` instance key-value pairs.
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema(
... {
... "category": pa.Column(str),
... "probability": pa.Column(float),
... }
... )
>>> print(
... example_schema.add_columns({"even_number": pa.Column(pa.Bool)})
... )
<Schema DataFrameSchema(
columns={
'category': <Schema Column(name=category, type=DataType(str))>
'probability': <Schema Column(name=probability, type=DataType(float64))>
'even_number': <Schema Column(name=even_number, type=DataType(bool))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
.. seealso:: :func:`remove_columns`
"""
schema_copy = copy.deepcopy(self)
schema_copy.columns = {
**schema_copy.columns,
**self.__class__(extra_schema_cols).columns,
}
return cast(DataFrameSchema, schema_copy)
@inferred_schema_guard
def remove_columns(self, cols_to_remove: List[str]) -> "DataFrameSchema":
"""Removes columns from a :class:`DataFrameSchema` and returns a new
copy.
:param cols_to_remove: Columns to be removed from the
``DataFrameSchema``
:type cols_to_remove: List
:returns: a new :class:`DataFrameSchema` without the cols_to_remove
:raises: :class:`~pandera.errors.SchemaInitError`: if column not in
schema.
:example:
To remove a column or set of columns from a schema, pass a list of
columns to be removed:
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema(
... {
... "category" : pa.Column(str),
... "probability": pa.Column(float)
... }
... )
>>>
>>> print(example_schema.remove_columns(["category"]))
<Schema DataFrameSchema(
columns={
'probability': <Schema Column(name=probability, type=DataType(float64))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
.. seealso:: :func:`add_columns`
"""
schema_copy = copy.deepcopy(self)
# ensure all specified keys are present in the columns
not_in_cols: List[str] = [
x for x in cols_to_remove if x not in schema_copy.columns.keys()
]
if not_in_cols:
raise errors.SchemaInitError(
f"Keys {not_in_cols} not found in schema columns!"
)
for col in cols_to_remove:
schema_copy.columns.pop(col)
return cast(DataFrameSchema, schema_copy)
@inferred_schema_guard
def update_column(self, column_name: str, **kwargs) -> "DataFrameSchema":
"""Create copy of a :class:`DataFrameSchema` with updated column
properties.
:param column_name:
:param kwargs: key-word arguments supplied to
:class:`~pandera.api.pandas.components.Column`
:returns: a new :class:`DataFrameSchema` with updated column
:raises: :class:`~pandera.errors.SchemaInitError`: if column not in
schema or you try to change the name.
:example:
Calling ``schema.1`` returns the :class:`DataFrameSchema`
with the updated column.
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema({
... "category" : pa.Column(str),
... "probability": pa.Column(float)
... })
>>> print(
... example_schema.update_column(
... 'category', dtype=pa.Category
... )
... )
<Schema DataFrameSchema(
columns={
'category': <Schema Column(name=category, type=DataType(category))>
'probability': <Schema Column(name=probability, type=DataType(float64))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
.. seealso:: :func:`rename_columns`
"""
# check that columns exist in schema
schema = self
if "name" in kwargs:
raise ValueError("cannot update 'name' of the column.")
if column_name not in schema.columns:
raise ValueError(f"column '{column_name}' not in {schema}")
schema_copy = copy.deepcopy(schema)
column_copy = copy.deepcopy(schema.columns[column_name])
new_column = column_copy.__class__(
**{**column_copy.properties, **kwargs}
)
schema_copy.columns.update({column_name: new_column})
return cast(DataFrameSchema, schema_copy)
def update_columns(
self,
update_dict: Dict[str, Dict[str, Any]],
) -> "DataFrameSchema":
"""
Create copy of a :class:`DataFrameSchema` with updated column
properties.
:param update_dict:
:return: a new :class:`DataFrameSchema` with updated columns
:raises: :class:`~pandera.errors.SchemaInitError`: if column not in
schema or you try to change the name.
:example:
Calling ``schema.update_columns`` returns the :class:`DataFrameSchema`
with the updated columns.
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema({
... "category" : pa.Column(str),
... "probability": pa.Column(float)
... })
>>>
>>> print(
... example_schema.update_columns(
... {"category": {"dtype":pa.Category}}
... )
... )
<Schema DataFrameSchema(
columns={
'category': <Schema Column(name=category, type=DataType(category))>
'probability': <Schema Column(name=probability, type=DataType(float64))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
"""
# pylint: disable=import-outside-toplevel,import-outside-toplevel
from pandera.api.pandas.components import Column
new_schema = copy.deepcopy(self)
# ensure all specified keys are present in the columns
not_in_cols: List[str] = [
x for x in update_dict.keys() if x not in new_schema.columns.keys()
]
if not_in_cols:
raise errors.SchemaInitError(
f"Keys {not_in_cols} not found in schema columns!"
)
new_columns: Dict[str, Column] = {}
for col in new_schema.columns:
# check
if update_dict.get(col):
if update_dict[col].get("name"):
raise errors.SchemaInitError(
"cannot update 'name' \
property of the column."
)
original_properties = new_schema.columns[col].properties
if update_dict.get(col):
new_properties = copy.deepcopy(original_properties)
new_properties.update(update_dict[col])
new_columns[col] = new_schema.columns[col].__class__(
**new_properties
)
else:
new_columns[col] = new_schema.columns[col].__class__(
**original_properties
)
new_schema.columns = new_columns
return cast(DataFrameSchema, new_schema)
def rename_columns(self, rename_dict: Dict[str, str]) -> "DataFrameSchema":
"""Rename columns using a dictionary of key-value pairs.
:param rename_dict: dictionary of 'old_name': 'new_name' key-value
pairs.
:returns: :class:`DataFrameSchema` (copy of original)
:raises: :class:`~pandera.errors.SchemaInitError` if column not in the
schema.
:example:
To rename a column or set of columns, pass a dictionary of old column
names and new column names, similar to the pandas DataFrame method.
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema({
... "category" : pa.Column(str),
... "probability": pa.Column(float)
... })
>>>
>>> print(
... example_schema.rename_columns({
... "category": "categories",
... "probability": "probabilities"
... })
... )
<Schema DataFrameSchema(
columns={
'categories': <Schema Column(name=categories, type=DataType(str))>
'probabilities': <Schema Column(name=probabilities, type=DataType(float64))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
.. seealso:: :func:`update_column`
"""
new_schema = copy.deepcopy(self)
# ensure all specified keys are present in the columns
not_in_cols: List[str] = [
x for x in rename_dict.keys() if x not in new_schema.columns.keys()
]
if not_in_cols:
raise errors.SchemaInitError(
f"Keys {not_in_cols} not found in schema columns!"
)
# remove any mapping to itself as this is a no-op
rename_dict = {k: v for k, v in rename_dict.items() if k != v}
# ensure all new keys are not present in the current column names
already_in_columns: List[str] = [
x for x in rename_dict.values() if x in new_schema.columns.keys()
]
if already_in_columns:
raise errors.SchemaInitError(
f"Keys {already_in_columns} already found in schema columns!"
)
# We iterate over the existing columns dict and replace those keys
# that exist in the rename_dict
new_columns = {
(rename_dict[col_name] if col_name in rename_dict else col_name): (
col_attrs.set_name(rename_dict[col_name])
if col_name in rename_dict
else col_attrs
)
for col_name, col_attrs in new_schema.columns.items()
}
new_schema.columns = new_columns
return cast(DataFrameSchema, new_schema)
def select_columns(self, columns: List[Any]) -> "DataFrameSchema":
"""Select subset of columns in the schema.
*New in version 0.4.5*
:param columns: list of column names to select.
:returns: :class:`DataFrameSchema` (copy of original) with only
the selected columns.
:raises: :class:`~pandera.errors.SchemaInitError` if column not in the
schema.
:example:
To subset a schema by column, and return a new schema:
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema({
... "category" : pa.Column(str),
... "probability": pa.Column(float)
... })
>>>
>>> print(example_schema.select_columns(['category']))
<Schema DataFrameSchema(
columns={
'category': <Schema Column(name=category, type=DataType(str))>
},
checks=[],
coerce=False,
dtype=None,
index=None,
strict=False,
name=None,
ordered=False,
unique_column_names=False,
metadata=None,
add_missing_columns=False
)>
.. note:: If an index is present in the schema, it will also be
included in the new schema.
"""
new_schema = copy.deepcopy(self)
# ensure all specified keys are present in the columns
not_in_cols: List[str] = [
x for x in columns if x not in new_schema.columns.keys()
]
if not_in_cols:
raise errors.SchemaInitError(
f"Keys {not_in_cols} not found in schema columns!"
)
new_columns = {
col_name: column
for col_name, column in self.columns.items()
if col_name in columns
}
new_schema.columns = new_columns
return cast(DataFrameSchema, new_schema)
def set_index(
self, keys: List[str], drop: bool = True, append: bool = False
) -> "DataFrameSchema":
"""
A method for setting the :class:`Index` of a :class:`DataFrameSchema`,
via an existing :class:`Column` or list of columns.
:param keys: list of labels
:param drop: bool, default True
:param append: bool, default False
:return: a new :class:`DataFrameSchema` with specified column(s) in the
index.
:raises: :class:`~pandera.errors.SchemaInitError` if column not in the
schema.
:examples:
Just as you would set the index in a ``pandas`` DataFrame from an
existing column, you can set an index within the schema from an
existing column in the schema.
>>> import pandera as pa
>>>
>>> example_schema = pa.DataFrameSchema({
... "category" : pa.Column(str),
... "probability": pa.Column(float)})
>>>
>>> print(example_schema.set_index(['category']))
<Schema DataFrameSchema(
columns={