Search code examples
pythonpandasdataframeplotly

How to include a specific date to a datetime column in pandas?


I have a csv file that has two columns: one for date and one for time. When processing it with pandas, I do the following:

df= pd.read_csv(csv_file, low_memory=False, sep=";")
df["Date"] = pd.to_datetime(df["Date"], format="%d/%m/%Y", exact=False)
df["Time"] = pd.to_datetime(df["Time"], format="%H:%M:%S", exact=False)

Which results in df["Time"] being this:

0        1900-01-01 10:01:46

1        1900-01-01 10:01:47

2        1900-01-01 10:01:48

3        1900-01-01 10:01:50

4        1900-01-01 10:01:51

. . .

That makes my plotly script x axis be the following:

x axis in my plot

How can I add the correct date to the Time column (which I have stored in my df["Date"] column)?

I have tried to do like in here:

df['Date'] + ' ' + df['Time']

But it didin't work and I got the following

TypeError: unsupported operand type(s) for +: 'DatetimeArray' and 'str'


Solution

  • Example Code

    We need a minimal and reproducible example to answer. Let's create a brief one.

    import io
    import pandas
    
    csv1 = '''Date;Time
    2022-05-01;10:01:46
    2022-05-02;10:01:47
    2022-05-03;10:01:48
    2022-05-04;10:01:50
    2022-05-05;10:01:51
    '''
    df = pd.read_csv(io.StringIO(csv1), sep=';')
    

    df

             Date      Time
    0  2022-05-01  10:01:46
    1  2022-05-02  10:01:47
    2  2022-05-03  10:01:48
    3  2022-05-04  10:01:50
    4  2022-05-05  10:01:51
    

    Code

    Based on your comment, the Date column is assumed to be datetime and the Time column is assumed to be str. However, this is not certain, so let's change the Date column back to datetime and the Time column to Timedelta and merge them.

    out = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'])
    

    out:

    0   2022-05-01 10:01:46
    1   2022-05-02 10:01:47
    2   2022-05-03 10:01:48
    3   2022-05-04 10:01:50
    4   2022-05-05 10:01:51
    dtype: datetime64[ns]