<p>这里有一个,有点类似于<a href="https://github.com/TomAugspurger/engarde/blob/master/engarde/decorators.py" rel="nofollow">^{<cd1>}</a></p>
<pre><code>In [84]: def has_datetimeindex(func):
...: @wraps(func)
...: def wrapper(df, *args, **kwargs):
...: assert isinstance(df.index, pd.DatetimeIndex)
...: return func(df, *args, **kwargs)
...: return wrapper
In [85]: @has_datetimeindex
...: def f(df):
...: return df + 1
In [86]: df = pd.DataFrame({'a':[1,2,3]})
In [87]: f(df)
-
AssertionError Traceback (most recent call last)
<ipython-input-87-ce83b19059ea> in <module>()
> 1 f(df)
<ipython-input-84-1ecf9973e7d5> in wrapper(df, *args, **kwargs)
2 @wraps(func)
3 def wrapper(df, *args, **kwargs):
> 4 assert isinstance(df.index, pd.DatetimeIndex)
5 return func(df, *args, **kwargs)
6 return wrapper
AssertionError:
In [88]: df = pd.DataFrame({'a':[1,2,3]}, index=pd.date_range('2014-1-1', periods=3))
In [89]: f(df)
Out[89]:
a
2014-01-01 2
2014-01-02 3
2014-01-03 4
</code></pre>