Home > Software engineering >  Importing dates in dd.mm.yyy format from csv in jupyter notebook
Importing dates in dd.mm.yyy format from csv in jupyter notebook

Time:09-28

Data looks like this in Excel/CSV

I have a large CSv file (80 000 rows) that has one column with dates in dd.mm.yyyy format and one column with time in hh:mm:ss format and I try to plt a graph that has date and time on the x-axis.

I have tried the parse_dates function, but that one uses yyyy-mm-dd and converts some to that format but some not.

Dates go from 9th of August to 18th of August.

1       2021-09-08
2       2021-09-08
3       2021-09-08
4       2021-09-08
           ...    
83771   2021-08-18
83772   2021-08-18
83773   2021-08-18
83774   2021-08-18
83775   2021-08-18

I can use a string but from what I saw it takes way longer to plot a graph with a string as x-axis, so I try to avoid that.

Is there a way to combine date and time and save it as datetime in any consistent format and save not as an object?

Thanks in advance.

CodePudding user response:

try use Pandas:

example: enter image description here


import pandas as pd

df = pd.read_csv('date.csv',parse_datas = ['date'])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['monthDay'] = df['date'].dt.day
df['weekDay'] = df['date'].dt.weekday

print(df)

It can be modified as required.

CodePudding user response:

I have found a way, so if anyone else is ever looking for this, here it is

datetime= Date "/" Time

Datum=pd.to_datetime(datetime, format="%d.%m.%Y/%H:%M:%S")

print(Datum)

0       2021-08-09 00:00:00
1       2021-08-09 00:00:10
2       2021-08-09 00:00:20
3       2021-08-09 00:00:30
4       2021-08-09 00:00:40
                ...        
83771   2021-08-18 17:22:30
83772   2021-08-18 17:22:40
83773   2021-08-18 17:22:50
83774   2021-08-18 17:23:00
83775   2021-08-18 17:23:10
Length: 83776, dtype: datetime64[ns]´´´
  • Related