Home > database >  Combining data and time colums with panda.read_csv
Combining data and time colums with panda.read_csv

Time:05-19

I have two diferent colums thats, together, form a full data. The first it data (D/M/Y) and the second is time (H:M:S), and I need to read it as a full data (D/M/Y H:M:S).

What I tried is:

mydateparser = lambda x: datetime.strptime(x, "%d/%m/%Y")
dfim = pd.read_csv('Dates_images.csv',
                      header = 0,
                      sep = ';',
                      usecols = ['Date','Time','Satellite'],
                      parse_dates = ['Date'],
                      date_parser = mydateparser)


dt = datetime.date('Date')
tm = datetime.time('Time')

dfim['FullDate'] = datetime.combine(dt, tm)

print(dfim)

But it's not working 'couse it doesn't understand the 'Time' as a time data...

CodePudding user response:

Consider passing a list of lists (or dict of list for renaming result) into parse_dates of pandas.read_csv. Note: nested list of list should be passed not single list for multiple datetimes. Per docs:

parse_dates: bool or list of int or names or list of lists or dict, default False
The behavior is as follows:

  • boolean. If True -> try parsing the index.
  • list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
  • list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
  • dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’
mydateparser = lambda x: datetime.strptime(x, "%d/%m/%Y H:M:S")

dfim = pd.read_csv(
    'Dates_images.csv', 
    header = 0, 
    sep = ';', 
    usecols = ['Date','Time','Satellite'], 
    parse_dates = {"Full Time": ['Date', 'Time']}, 
    date_parser = mydateparser
)

CodePudding user response:

You can try

df['Full Time'] = df['Date']   df['Time']
df['Full Time'] = pd.to_datetime(df['Full Time'], format = '%Y-%m-%d %H:%M:%S')
  • Related