如何将变量设置为“今天" Python/Pandas中的日期 [英] How to set a variable to be "Today's" date in 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')
Timestamp('2015-10-14 00:00:00')
无论实际时间如何,它总是返回午夜的今天的日期(但请猜测参数now
将执行什么操作).
This will always return today's date at midnight, irrespective of the actual time (but guess what the argument now
will do).
这篇关于如何将变量设置为“今天" Python/Pandas中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!