`pd.Timestamp.now()`和`datetime.datetime.now()`都是用来获取当前时间的函数,但它们之间存在一些差异¹²。 - `pd.Timestamp.now()`返回
  X5zJxoD00Cah 2023年11月30日 15 0

`pd.Timestamp.now()`和`datetime.datetime.now()`都是用来获取当前时间的函数,但它们之间存在一些差异¹²。


- `pd.Timestamp.now()`返回的是Pandas的Timestamp对象,这个对象是在UTC(协调世界时)时区下的当前时间¹²。

- `datetime.datetime.now()`返回的是Python的datetime对象,这个对象是在本地时区下的当前时间²。


所以,如果你在一个非UTC时区下运行这两个函数,你会发现它们返回的时间并不相同。这是因为`pd.Timestamp.now()`总是返回UTC时间,而`datetime.datetime.now()`则返回本地时间²。


如果你想要在Pandas中获取本地时间,你可以使用`pd.Timestamp.today()`³。同样,如果你想要在datetime中获取UTC时间,你可以使用`datetime.datetime.utcnow()`⁴。


`pd.Timestamp.today()`和`pd.Timestamp.now()`都是Pandas库中用于获取当前时间的函数,但它们的行为略有不同¹²。


- `pd.Timestamp.now()`返回的是当前的日期和时间(包括小时、分钟和秒),并且这个时间是在本地时区下的¹²。

- `pd.Timestamp.today()`也返回的是当前的日期和时间,但是它可以被本地化到一个传递的时区¹。如果没有指定时区,那么它将返回本地时区下的当前时间¹。


所以,如果你在一个非UTC时区下运行这两个函数,你会发现它们返回的时间是相同的⁴。希望这个解释对你有所帮助!


源: 与必应的对话, 2023/11/30

在Python中,你可以使用Pandas、datetime和str()函数将时间戳转换为字符串,但它们的行为略有不同¹²³⁴⁵。

- `pd.Timestamp.strftime()`:这是Pandas库中的一个方法,它可以将Timestamp对象转换为字符串¹³⁴。你可以传递一个格式字符串给`strftime()`,它会返回一个格式化的字符串¹³⁴。例如,`'%Y-%m-%d %H:%M:%S'`会将时间戳转换为"年-月-日 时:分:秒"的格式¹³⁴。

- `datetime.datetime.strftime()`:这是Python的datetime模块中的一个方法,它的行为与`pd.Timestamp.strftime()`类似²。你可以传递一个格式字符串给`strftime()`,它会返回一个格式化的字符串²。例如,`'%Y-%m-%d %H:%M:%S'`会将时间戳转换为"年-月-日 时:分:秒"的格式²。

- `str()`:这是Python的内置函数,它可以将几乎所有类型的对象转换为字符串⁵。当你将它应用到一个时间戳上时,它会返回一个包含时间戳完整信息的字符串⁵。这个字符串的格式可能与使用`strftime()`得到的字符串不同,因为`str()`并不提供格式化选项⁵。

希望这个解释对你有所帮助!

源: 与必应的对话, 2023/11/30
(1) python - pandas timestamp series to string? - Stack Overflow. https://stackoverflow.com/questions/44741587/pandas-timestamp-series-to-string.
(2) pandas - How to convert timestamp into string in Python - Stack Overflow. https://stackoverflow.com/questions/55941595/how-to-convert-timestamp-into-string-in-python.
(3) How to convert a timestamp to string in pandas? - EasyTweaks.com. https://www.easytweaks.com/convert-timestamp-string-pandas/.
(4) How to Convert DateTime to String in Pandas (With Examples). https://www.statology.org/pandas-convert-datetime-to-string/.
(5) Pandas如何将Python中的时间戳转换为字符串 - 极客教程. https://geek-docs.com/pandas/pandas-questions/545_pandas_how_to_convert_timestamp_into_string_in_python.html.

(1) pandas.Timestamp.today — pandas 2.1.3 documentation. https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.today.html.

(2) pandas.Timestamp — pandas 2.1.3 documentation. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timestamp.html.

(3) python - Get today's date in Pandas - Stack Overflow. https://stackoverflow.com/questions/57365072/get-todays-date-in-pandas.

(4) Timestamp("now") should match to_datetime("now") #18705 - GitHub. https://github.com/pandas-dev/pandas/issues/18705.

源: 与必应的对话, 2023/11/30

(1) Converting between datetime and Pandas Timestamp objects. https://stackoverflow.com/questions/22825349/converting-between-datetime-and-pandas-timestamp-objects.

(2) Difference between pandas Timestamp and datetime. https://stackoverflow.com/questions/66049738/difference-between-pandas-timestamp-and-datetime.

(3) Timestamp("now") should match to_datetime("now") #18705 - GitHub. https://github.com/pandas-dev/pandas/issues/18705.

(4) Why the difference between datetime.datetime.now().timestamp() and .... https://stackoverflow.com/questions/49819691/why-the-difference-between-datetime-datetime-now-timestamp-and-datetime-date.

(5) Why does pandas return timestamps instead of datetime objects when .... https://stackoverflow.com/questions/23755146/why-does-pandas-return-timestamps-instead-of-datetime-objects-when-calling-pd-to.

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月30日 0

暂无评论

推荐阅读
  X5zJxoD00Cah   2023年12月11日   25   0   0 知乎Python迭代器
  X5zJxoD00Cah   2023年12月12日   30   0   0 Python.net
X5zJxoD00Cah