python-3.xdatetimestrptimedateparser

Issue with date_parser: TypeError: parse() takes 1 positional argument but 2 were given


I have got the following error:

TypeError: parse() takes 1 positional argument but 2 were given

I was trying to do a basic data preparation where I wanted to parse the date-time information as a Panda DataFrame index (combine the 'date' and 'time' columns together in a single column). This is a snippet of the code:

from pandas import read_csv
from datetime import datetime
def parse(x):
    return datetime.strptime(x,'%d-%b-%y %H:%M:%S' )

dataset = read_csv("dataset.csv", header=0, parse_dates = [['date', 'time']],
                   index_col=0, date_parser= parse)

This is how the original date and time look like:

date          time
25-Apr-17   19:19:40
25-Apr-17   19:19:40
25-Apr-17   19:19:45
25-Apr-17   19:19:45

I came across an alternative way to use:

dataset = read_csv("dataset.csv", header=0, parse_dates = {'datetime':[1,2]},
                   index_col=0, date_parser=lambda x: datetime.strptime(x,'%d-%b-%y %H:%M:%S' )

But still the same issue. TypeError: <lambda>() takes 1 positional argument but 2 were given

I was wondering if you guys could help me with this issue?


Solution

  • After a couple of a trail and error, I eventually managed to resolve the issue. I used pd.to_datetime instead of a datetime.strptime.

    from pandas import read_csv
    from datetime import datetime
    import pandas as pd
    def parse(d, t):
        dt = d+ " " +t
        return pd.to_datetime(dt)
    
    dataset = read_csv("dataset.csv", header=0, parse_dates={'datetime': ['date', 'time']},
                       index_col=0, date_parser= parse)
    

    The output:

    datetime
    2017-04-25 19:19:40
    2017-04-25 19:19:40
    2017-04-25 19:19:45
    2017-04-25 19:19:45
    

    I double checked the datatype of the 'date' and 'time', they were of datatype 'object'. I am not sure if this method would work with other datatypes such as a string but it solves my problem.

    Thank you everyone for your participation.