如何将变量设置为“今天的"?Python/Pandas 中的日期 [英] How to set a variable to be "Today's" date in Python/Pandas

查看:36
本文介绍了如何将变量设置为“今天的"?Python/Pandas 中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量设置为今天的日期.

I am trying to set a variable to equal today's date.

我查了一下,找到了一篇相关的文章:

I looked this up and found a related article:

将今天的日期设置为模型中的默认值

但是,这并没有特别回答我的问题.

However, this didn't particularly answer my question.

我使用了建议的:

dt.date.today

但是之后

import datetime as dt     
date = dt.date.today
print date
 <built-in method today of type object at 0x000000001E2658B0>

 Df['Date'] = date

我没有得到我真正想要的作为今天日期的干净日期格式......月/日/年.

I didn't get what I actually wanted which as a clean date format of today's date...in Month/Day/Year.

如何创建今天的变量,以便在 DataFrame 中输入该变量?

How can I create a variable of today's day in order for me to input that variable in a DataFrame?

推荐答案

你提到你正在使用 Pandas(在你的标题中).如果是这样,则无需使用外部库,您可以使用 to_datetime

You mention you are using Pandas (in your title). If so, there is no need to use an external library, you can just use to_datetime

>>> pandas.to_datetime('today').normalize()
Timestamp('2015-10-14 00:00:00')

无论实际时间如何,这总是会在午夜返回今天的日期,并且可以直接在 pandas 中用于进行比较等.Pandas 的日期时间中总是包含 00:00:00.

This will always return today's date at midnight, irrespective of the actual time, and can be directly used in pandas to do comparisons etc. Pandas always includes 00:00:00 in its datetimes.

today 替换为 now 将为您提供 UTC 日期而不是本地时间;请注意,在这两种情况下都不会添加 tzinfo(时区).

Replacing today with now would give you the date in UTC instead of local time; note that in neither case is the tzinfo (timezone) added.

在 0.23.x 之前的 pandas 版本中,可能不需要 normalize 来删除非午夜时间戳.

In pandas versions prior to 0.23.x, normalize may not have been necessary to remove the non-midnight timestamp.

这篇关于如何将变量设置为“今天的"?Python/Pandas 中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆