python - 熊猫日期时间:加载到熊猫数据框时的年份格式问题
问题描述
我的数据集如下:
Sl.No Date1
1 08-09-1990
2 01-06-1988
3 04-10-1989
4 15-11-1991
5 01-06-1968
当我尝试加载数据时:
df = pd.read_csv("file",parse_dates=True, dayfirst=True)
我得到的输出为:
0 08-09-90
1 01-06-88
2 04-10-89
3 15-11-91
4 01-06-68
问题是:
- 日期格式是dd-mm-YY而不是dd-mm-YYYY
- 结果,当我尝试转换日期时间格式时,1968 年被视为 2068 年(例如,输出中的索引 4/输入中的 sl.no 5)
也根据建议的链接[如何在 read_csv 中指定日期时间格式
我试过这是和以前一样的问题
我也试过 [时间数据与格式不匹配
df=pd.read_csv("文件",infer_datetime_format=True) df[Date1]=pd.to_datetime(df['Date1'], format='%d-%m-%Y')
我面临 ValueError '08-09-90' 与格式 '%d-%m-%Y' 不匹配
解决方案
试一试 - 它似乎对我有用
import pandas as pd
filepath = '' # insert your files path here (I created a csv with columns 'SI_No' and 'Date' to test this and then copied your data)
df = pd.read_csv(filepath, parse_dates=['Date'])
df = df.set_index('SI_No')
df
Date
SI_No
1 1990-08-09
2 1988-01-06
3 1989-04-10
4 1991-11-15
5 1968-01-06
推荐阅读
- c# - 托管 Blazor Web 程序集 - Bearer error="invalid_token", error_description="未找到签名密钥"
- python - 使用 Boto3 Lambda 函数的 S3 存储桶生命周期配置并出现 MalformedXML 格式错误
- kubernetes-helm - 如何让 helm hooks 只重试一次?
- node.js - 如何在useEffect反应中停止内存泄漏
- arrays - VHDL 中的无符号加法导致不正确的长度无符号结果
- reactjs - 如何在使用 React Infinite Scroll Component 时永久显示 Y 滚动条?
- ios - Flutter 应用程序未在 IOS 模拟器上运行或构建
- server - AEDES 服务器未连接到客户端
- javascript - 需要帮助在 Id 内的类内查找文本
- matlab - Matlab 2.5D Delaunay三角剖分顶点法线