pythonpandasdatetimetime-difference

Pandas create date time column based on other column with relative milliseconds time


My original data csv file columns looks like this:

| Millisec_diff | Value1 | Value2 |
| ------------- | ------ | ------ |
| 0             |  100   | 200    |
| 0.005         |  101   |  20.1  |
| 0.01          |  103   |  20.1  |
| 0.015         |  104   |  24.1  |
| 0.02          |  103   |  40.1  |

I would like to create a column with date time in ISO 8601 format based on current time + timedelta based on Millisec_diff.

I tried:

start = datetime.today()
df['time'] = (
            datetime(start.year, start.month, start.day,start.hour, start.minute, start.second, start.microsecond)
            + pandas.to_timedelta(df['Millisec_diff '], unit='ms')
        )

Which generated a time column, but value is the same. I wanted to generate time with difference as per Millisec_diff. Thanks.


Solution

  • If your intention is fraction of milliseconds, then your code right. If not, the main problem on your code is that your Millisecond diff column is not integer, 0.005 millisecond means 000005 microseconds. So you have to multiply your millisecond values to 1000 to get actual milliseconds. What is more, microseconds comes after milliseconds, so you can (or should) remove microseconds from your code.

    from datetime import datetime
    import pandas as pd
    
    # Sample data
    data = {
        'Millisec_diff': [0, 0.005, 0.01, 0.015, 0.02],
        'Value1': [100, 101, 103, 104, 103],
        'Value2': [200, 20.1, 20.1, 24.1, 40.1]
    }
    
    df = pd.DataFrame(data)
    
    df['Millisec_diff'] = df['Millisec_diff']*1000
    start = datetime.today()
    df['time'] = (
                datetime(start.year, start.month, start.day,start.hour, start.minute, start.second)
                + pd.to_timedelta(df['Millisec_diff'], unit='ms')
            )
    

    This should work.