Skip to main content
 首页 » 编程设计

python之pandas.Timestamp 是不可变的吗

2024年09月07日7zhujiabin

我自己还没有找到任何方法来改变现有的 pandas.Timestamp,但也没有找到任何明确声明它是不可变的文档。

有人知道答案或有办法找出答案吗?

请您参考如下方法:

查看源代码后,我发现继承自 datetime.datetime 是不可变的。

# in pandas/_lib/tslibs/timestamp.pyx 
cdef class _Timestamp(datetime): 
# ... 
class Timestamp(_Timestamp): # This is the class that is exported 

如果你查看 datetime 的 python 实现,你会发现它应该是不可变的(通过 onway 属性):

# Read-only field accessors 
@property 
def year(self): 
    """year (1-9999)""" 
    return self._year 
 
@property 
def month(self): 
    """month (1-12)""" 
    return self._month 
 
@property 
def day(self): 
    """day (1-31)""" 
    return self._day 
 
@property 
def hour(self): 
    """hour (0-23)""" 
    return self._hour 
 
@property 
def minute(self): 
    """minute (0-59)""" 
    return self._minute 
 
@property 
def second(self): 
    """second (0-59)""" 
    return self._second 
 
@property 
def microsecond(self): 
    """microsecond (0-999999)""" 
    return self._microsecond