2017-01-12 15 views
0

3番目のオブジェクトがタイムスタンプオブジェクトの下にあるのはなぜですか?私はTimeオブジェクトを期待していました。ここでpandas.Timestampをサブクラス化する方法?

import pandas as pd 
from datetime import datetime 

class Time(pd.Timestamp): 
    def __new__(cls, *args, **kwargs): 
     return pd.Timestamp.__new__(cls, *args, **kwargs) 

print type(Time(datetime(2012, 5, 1))) 
print type(Time('2012-05-01')) 
print type(Time(2012, 5, 1)) 

は私のpython 2.7.11とパンダ0.19.0から見た結果は以下のとおりです。

<class '__main__.Time'> 
<class '__main__.Time'> 
<class 'pandas.tslib.Timestamp'> 

答えて

0

私はこれがあなたの質問に答えていない知っているが、FYI私は

<class '__main__.Time'> 
を取得します

すべてpython 2.7.13とpandas 0.18.1で3つ。

あなたが見る動作は0.19に導入されました。

Hereが該当するコードの変更です。

0

これは動作しますが、優雅な世界でpandas.Timestamp.__new__は私のために、クラスの割り当てを行うだろう:

import pandas as pd 
from datetime import datetime 

class Time(pd.Timestamp): 
    def __new__(cls, *args, **kwargs): 
     time = pd.Timestamp.__new__(cls, *args, **kwargs) 
     time.__class__ = cls 
     return time 

print type(Time(datetime(2012, 5, 1))) 
print type(Time('2012-05-01')) 
print type(Time(2012, 5, 1)) 
関連する問題