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.
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
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.
output:
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
Here is my code :
I loop through files :
df = pd.read_csv(PATH+file, header = None,error_bad_lines=False , sep = '\t')
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)
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)
But it doesn't work either