Python Pandas:Pandas.to_datetime()正在切换日期&;天小于13的月份
我写了一个代码,可以读取多个文件,但是在我的一些文件上,只要日期小于13,datetime交换day&month,并且从第13天开始的任何一天(即11年6月13日)都保持正确(DD/MM/YY)。 我试图通过这样做来修复它,但它不起作用 我的数据框如下所示: 实际日期时间为2015年6月12日至2015年6月13日 当我将日期时间列作为字符串读取时,日期在dd/mm/yyyy保持正确Python Pandas:Pandas.to_datetime()正在切换日期&;天小于13的月份,python,pandas,datetime,dataframe,series,Python,Pandas,Datetime,Dataframe,Series,我写了一个代码,可以读取多个文件,但是在我的一些文件上,只要日期小于13,datetime交换day&month,并且从第13天开始的任何一天(即11年6月13日)都保持正确(DD/MM/YY)。 我试图通过这样做来修复它,但它不起作用 我的数据框如下所示: 实际日期时间为2015年6月12日至2015年6月13日 当我将日期时间列作为字符串读取时,日期在dd/mm/yyyy保持正确 tmp p1 p2 11/06/2015 00:56:55.060
tmp p1 p2
11/06/2015 00:56:55.060 0 1
11/06/2015 04:16:38.060 0 1
12/06/2015 16:13:30.060 0 1
12/06/2015 21:24:03.060 0 1
13/06/2015 02:31:44.060 0 1
13/06/2015 02:37:49.060 0 1
但当我将列的类型更改为datetime列时,它会将我的日期和月份替换为少于13天的每一天
输出:
print(df)
tmp p1 p2
06/11/2015 00:56:55 0 1
06/11/2015 04:16:38 0 1
06/12/2015 16:13:30 0 1
06/12/2015 21:24:03 0 1
13/06/2015 02:31:44 0 1
13/06/2015 02:37:49 0 1
这是我的代码:
我循环浏览文件:
df = pd.read_csv(PATH+file, header = None,error_bad_lines=False , sep = '\t')
然后,当我的代码读取完我所有的文件后,我将它们连接起来,问题是我的datetime列需要是datetime类型,因此当我通过pd_datetime()更改其类型时,它会在日期小于13时交换日期和月份
转换日期时间列后日期是否正确(字符串类型)
但当我更改列类型时,我得到以下结果:
tmp = pd.to_datetime(tmp, unit = "ns")
tmp = temps_absolu.apply(lambda x: x.replace(microsecond=0))
print(tmp) # I get 06-11-2016 12:56:05 (06november2015 its not the right date)
问题是:当日数小于13时,我应该使用或更改什么命令来停止日和月交换
更新
此命令交换我的列的所有日期和月份
tmp = pd.to_datetime(tmp, unit='s').dt.strftime('%#m/%#d/%Y %H:%M:%S')
因此,为了只交换不正确的日期,我写了一个条件:
for t in tmp:
if (t.day < 13):
t = datetime(year=t.year, month=t.day, day=t.month, hour=t.hour, minute=t.minute, second = t.second)
对于tmp中的t:
如果(t.日<13):
t=日期时间(年=t.year,月=t.day,日=t.month,小时=t.hour,分钟=t.minute,秒=t.second)
但是它也不起作用您可以使用
pd.to\u datetime
中的dayfirst
参数
pd.to_datetime(df.tmp, dayfirst=True)
输出:
0 2015-06-11 00:56:55
1 2015-06-11 04:16:38
2 2015-06-12 16:13:30
3 2015-06-12 21:24:03
4 2015-06-13 02:31:44
5 2015-06-13 02:37:49
Name: tmp, dtype: datetime64[ns]
我解决了我的问题,但使用了一种消耗内存的方法,我首先将tmp列拆分为日期和时间列,然后将日期列重新拆分为日期、月份和年份,这样我就可以查找少于13天的天数,并用相应的月份替换它们
df['tmp'] = pd.to_datetime(df['tmp'], unit='ns')
df['tmp'] = df['tmp'].apply(lambda x: x.replace(microsecond=0))
df['date'] = [d.date() for d in df['tmp']]
df['time'] = [d.time() for d in df['tmp']]
df[['year','month','day']] = df['date'].apply(lambda x: pd.Series(x.strftime("%Y-%m-%d").split("-")))
df['day'] = pd.to_numeric(df['day'], errors='coerce')
df['month'] = pd.to_numeric(df['month'], errors='coerce')
df['year'] = pd.to_numeric(df['year'], errors='coerce')
#Loop to look for days less than 13 and then swap the day and month
for index, d in enumerate(df['day']):
if(d <13):
df.loc[index,'day'],df.loc[index,'month']=df.loc[index,'month'],df.loc[index,'day']
#在我们的列中合并时间、日期和地点结果
df['tmp'] =pd.to_datetime(df['date']+ ' '+df['time'])
#删除添加的列
df.drop(df[['date','year', 'month', 'day','time']], axis=1, inplace = True)
我遇到了同样的问题。在我的例子中,日期是索引列(称为“日期”)。上面提到的解决方案在索引列为“Date”的数据框上直接使用to_datetime()对我来说不起作用。我必须先使用read_csv(),而不将索引设置为“Date”,然后在其上应用_datetime(),然后才将索引设置为“Date”
你的问题是什么?问题是:我如何停止日月交换?为什么这不是公认的答案?非常好用,谢谢@戴奥尼索斯达芬奇谢谢。可能原始海报与此处记录的内容有不同的问题。如果指定了日期格式,是否会保留错误?@MiguelGonzalez不,不应该是Miguel,如果您使用的是%d/%m/%Y的精确格式。不会出现翻页错误。非常好,非常感谢@Scott Boston这很好。非常感谢您抽出时间!在循环过程中,pandas库中的KeyError 905不断出现。你知道怎么解决这个问题吗?提前谢谢
df['tmp'] =pd.to_datetime(df['date']+ ' '+df['time'])
df.drop(df[['date','year', 'month', 'day','time']], axis=1, inplace = True)
df= pd.read_csv(file, parse_dates=True)
df.Date = pd.to_datetime(df.Date, dayfirst=True)
df = df.set_index('Date')