Python Pandas:pandas.to_datetime()正在切换日期&小于13的月份 [英] Python Pandas : pandas.to_datetime() is switching day & month when day is less than 13

查看:167
本文介绍了Python Pandas:pandas.to_datetime()正在切换日期&小于13的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个读取多个文件的代码,但是在我的某些文件中,日期时间互换日&当日期小于13时,以及从13日开始或以后(即11/06/11)的任何一天都保持正确(DD/MM/YY). 我试图通过执行此操作来修复它,但是它不起作用.

I wrote a code that reads multiple files, however on some of my files datetime swaps day & month whenever the day is less than 13, and any day that is from day 13 or above i.e. 13/06/11 remains correct (DD/MM/YY). I tried to fix it by doing this,but it doesn't work.

我的数据框如下所示: 实际的日期时间是从2015年6月12日到2015年6月13日 当我将我的datetime列读取为字符串时,日期保持正确的dd/mm/yyyy

My data frame looks like this: The actual datetime is from 12june2015 to 13june2015 when my I read my datetime column as a string the dates remain correct dd/mm/yyyy

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的每一天.

but when I change the type of my column to datetime column it swaps my day and month for each day that is less than 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.

then when my code finish reading all my files I concatenat them, the problem is that my datetime column needs to be in a datetime type so when I change its type by pd_datetime() it swaps the day and month when the day is less than 13.

将日期时间列转换为正确的日期(字符串类型)

Post converting my datetime column the dates are correct (string type)

print(tmp) # as a result I get 11.06.2015 12:56:05 (11june2015)

但是当我更改列类型时,我得到了:

But when I change the column type I get this:

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天时,我应该使用或更改什么命令来停止日和月的交换?

The question is : What command should i use or change in order to stop day and month swapping when the day is less than 13?

更新 此命令交换我专栏的所有日期和月份

UPDATE This command swaps all the days and months of my column

tmp =  pd.to_datetime(tmp, unit='s').dt.strftime('%#m/%#d/%Y %H:%M:%S') 

为了只交换不正确的日期,我写了一个条件:

So in order to swap only the incorrect dates, I wrote a condition:

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列拆分为日期和时间列,然后将日期列重新拆分为年月日,这样一来,我可以查找少于13天的天数,并用相应的月份替换它们.

Well I solved my problem but in a memory consuming method, I split my tmp column first to a date and time columns then I re-split my date column to day month and year, that way I could look for the days that are less than 13 and replace them with the correspondent month

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'] 

#将系列转换为字符串类型以便合并它们

# convert series to string type in order to merge them

 df['day'] = df['day'].astype(str)
 df['month'] = df['month'].astype(str)
 df['year'] = df['year'].astype(str)
 df['date']=  pd.to_datetime(df[['year', 'month', 'day']])
 df['date'] = df['date'].astype(str)
 df['time'] = df['time'].astype(str)

#将时间,日期和位置结果合并到我们的列中

# merge time and date and place result in our column

df['tmp'] =pd.to_datetime(df['date']+ ' '+df['time'])

#删除添加的列

df.drop(df[['date','year', 'month', 'day','time']], axis=1, inplace = True)

这篇关于Python Pandas:pandas.to_datetime()正在切换日期&amp;小于13的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆