pandas 如何获得今天与熊猫的约会?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46575322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:35:20  来源:igfitidea点击:

How to get today's date with pandas?

pythonpandascsvdatedatetime

提问by ParalysisByAnalysis

I have a python script that imports a df, processes it, and then outputs to a csv file. I'm using pandas.write_csv()to output the file.

我有一个 python 脚本,它导入一个 df,处理它,然后输出到一个 csv 文件。我pandas.write_csv()用来输出文件。

I'm looking to standardize the manner in which my script names the output file. Specifically, I would like for the script to pull today's date as format: MMDDYYYY and insert that to the end of the CSV file.

我希望标准化我的脚本命名输出文件的方式。具体来说,我希望脚本将今天的日期提取为格式:MMDDYYYY 并将其插入到 CSV 文件的末尾。

outputfile = 'mypotentialfilename_MMDDYYYY.csv'
pd.write_csv(outputfile) 

In the past, I've seen quick ways to insert a date into a text, but I cannot seem to find the code to review. Any help is appreciated.

过去,我见过在文本中插入日期的快速方法,但我似乎找不到要查看的代码。任何帮助表示赞赏。

回答by piRSquared

python's formatcan handle date input

python的format可以处理日期输入

today = pd.Timestamp('today')
'filename_{:%m%d%Y}.csv'.format(today)

'filename_10042017.csv'

Using Python 3.6's f-strings

使用 Python 3.6 f-strings

today = pd.Timestamp('today')
f'filename_{today:%m%d%Y}.csv'

'filename_10042017.csv'

回答by cs95

You need dt.today+ dt.strftime:

你需要dt.today+ dt.strftime

import datetime as dt

today = dt.datetime.today().strftime('%m%d%Y')  
output_file = 'filename_{}.csv'.format(today)

Also, pd.write_csvis deprecated/removed in newer versions. I suggest upgrading your pandas to the latest version (v0.20 as of now), and using df.to_csv.

此外,pd.write_csv在较新版本中已弃用/删除。我建议将您的 Pandas 升级到最新版本(截至目前为 v0.20),并使用df.to_csv.