1
팬더 데이터 프레임을 입력 확인하고 싶습니다. 즉, DataFrame에 있어야 할 열 레이블과 어떤 종류의 데이터 유형 (dtype
)이 저장되어 있는지 지정하고 싶습니다.유형 검사 팬더 데이터 프레임
from collections import namedtuple
Col = namedtuple('Col', 'label, type')
def dataframe_check(*specification):
def check_accepts(f):
assert len(specification) <= f.__code__.co_argcount
def new_f(*args, **kwds):
for (df, specs) in zip(args, specification):
spec_columns = [spec.label for spec in specs]
assert (df.columns == spec_columns).all(), \
'Columns dont match specs {}'.format(spec_columns)
spec_dtypes = [spec.type for spec in specs]
assert (df.dtypes == spec_dtypes).all(), \
'Dtypes dont match specs {}'.format(spec_dtypes)
return f(*args, **kwds)
new_f.__name__ = f.__name__
return new_f
return check_accepts
나는 검사 기능의 복잡성을 꺼리지 않는다하지만 상용구 코드를 많이 추가합니다 (이 question에서 영감을)은 원유 구현은 다음과 같이 작동합니다.
@dataframe_check([Col('a', int), Col('b', int)], # df1
[Col('a', int), Col('b', float)],) # df2
def f(df1, df2):
return df1 + df2
f(df, df)
더 많은 데이터 검사 유형의 유형 체크가 있습니까? the new Python 3.6 static type-checking과 비슷한 모양인가요?
mypy에서 구현할 수 있습니까? 아마도
을 : //docs.python.org/2/library/collections.html#collections.OrderedDict) 열의 순서를 확인할 수도 있습니다. – joachim