python - 如何以 python 日期时间格式指定历史日期
问题描述
我有一个代码,该代码仅针对当前日期和时间执行。现在我需要为一个历史日期执行它,所以我需要替换下面提到的日期变量:
curr_month = dt.datetime.now()
和
today_day = dt.datetime.today()
输出
datetime.datetime(2020, 10, 2, 22, 36, 9, 155118)
我可以使用什么格式来指定历史日期,以便它采用上述确切格式的 DateTime 数据类型?
解决方案
在 pandas 中,可以根据您的需要使用datetime.datetime
或对象。pd.Timestamp
import pandas as pd
import datetime as dt
# datetime objects
historic_date = dt.datetime(2020,1,1)
date_str = '2020-01-01'
historic_date2 = dt.datetime.strptime(date_str, '%Y-%m-%d')
# Timestamp objects
historic_date3 = pd.to_datetime(date_str)
historic_date4 = pd.Timestamp(date_str)
historic_date5 = pd.Timestamp(historic_date)
推荐阅读
- android - 无法在 Visual Studio 2019 上启动 Android 模拟器
- reactjs - 为什么 React Js 被称为单页应用程序
- c++ - 在 C++ 中,将左值完美转发到函数模板的正确方法是什么?
- kong - 测试kong自定义插件发生dns服务器错误:3名称错误
- python - 解析嵌套字典时出错:平台和 sha256
- reactjs - 基于高度的材料UI纸张背景颜色?
- python - 如何在python中的列表之间添加一个字符串
- node.js - 为什么 npm install react-router-dom 不起作用?
- spring-boot - 如何在春季数据休息中允许蛇案例
- php - Woocommerce REST API 未返回默认类别