如何在 Python/Pandas 中将变量设置为“今天”日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21738566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a variable to be "Today's" date in Python/Pandas
提问by Alexis
I am trying to set a variable to equal today's date.
我试图将一个变量设置为等于今天的日期。
I looked this up and found a related article:
我查了一下,找到了一篇相关的文章:
Set today date as default value in the model
However, this didn't particularly answer my question.
但是,这并没有特别回答我的问题。
I used the suggested:
我使用了建议:
dt.date.today
But after
但是之后
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.
我没有得到我真正想要的作为今天日期的干净日期格式......在月/日/年。
How can I create a variable of today's day in order for me to input that variable in a DataFrame?
如何创建今天的变量以便我在 DataFrame 中输入该变量?
回答by jonrsharpe
If you want a string mm/dd/yyyyinstead of the datetimeobject, you can use strftime(string format time):
如果你想要一个字符串mm/dd/yyyy而不是datetime对象,你可以使用strftime(字符串格式时间):
>>> dt.datetime.today().strftime("%m/%d/%Y")
# ^ note parentheses
'02/12/2014'
回答by ashokdhudla
i got the same problem so tried so many things but finally this is the solution.
我遇到了同样的问题,所以尝试了很多东西,但最后这就是解决方案。
import time
print (time.strftime("%d/%m/%Y"))
回答by dirkjot
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
>>> pandas.to_datetime('today')
Timestamp('2015-10-14 00:00:00')
This will always return today's date at midnight, irrespective of the actual time (but guess what the argument nowwill do).
这将始终在午夜返回今天的日期,而不管实际时间如何(但猜猜参数now会做什么)。
回答by Ghostali
Easy solution in Python3+:
Python3+ 中的简单解决方案:
import time
todaysdate = time.strftime("%d/%m/%Y")
#with '.' isntead of '/'
todaysdate = time.strftime("%d.%m.%Y")
回答by Qijun Liu
import datetime
def today_date():
'''
utils:
get the datetime of today
'''
date=datetime.datetime.now().date()
date=pd.to_datetime(date)
return date
Df['Date'] = today_date()
this could be safely used in pandas dataframes.
这可以安全地用于熊猫数据帧。
回答by Cole Diamond
Using pandas: pd.Timestamp("today").strftime("%m/%d/%Y")
使用熊猫: pd.Timestamp("today").strftime("%m/%d/%Y")
回答by Jared Wilber
You can also look into pandas.Timestamp, which includes methods like .nowand .today.
Unlike pandas.to_datetime('now'), pandas.Timestamp.now()won't default to UTC:
您还可以查看pandas.Timestamp,其中包括像.now和这样的方法.today。与 不同pandas.to_datetime('now'),pandas.Timestamp.now()不会默认为 UTC:
import pandas as pd
pd.Timestamp.now() # will return California time
# Timestamp('2018-12-19 09:17:07.693648')
pd.to_datetime('now') # will return UTC time
# Timestamp('2018-12-19 17:17:08')
回答by sukesh kamath
pd.datetime.now().strftime("%d/%m/%Y")
pd.datetime.now().strftime("%d/%m/%Y")
this will give output as '11/02/2019'
这将使输出为“11/02/2019”
you can use add time if you want
如果需要,您可以使用添加时间
pd.datetime.now().strftime("%d/%m/%Y %I:%M:%S")
pd.datetime.now().strftime("%d/%m/%Y %I:%M:%S")
this will give output as '11/02/2019 11:08:26'
这将使输出为“11/02/2019 11:08:26”

