How to update "usual" datetime64[ns] values with some '<M8[ns]' values

Viewed 25

I know a bit about the different types of datetime objects in numpy but I don't fully understand it.

I want to use pandas.DataFrame.update() to update a timestamp column with some values of timestamps from another dataframe. The dtype of the "other" timestamps is <M8[ns]. When I update the original dataframe with that values the result are not timestamps but just big integers like 1444435200000000000.

The MWE does not reproduce the error because I don't know how to generate <M8[ns] values.

So the main question is how can I deal with that problem to update a timestamp column with timestamps of "different" types?

The secondary question would be how such <M8[ns] timestamps are generated? Maybe I can find the place in my code where they are created. Currently I just do pandas.to_datetime(arg=df.time_strings, format='%Y-%m-%d').

#!/usr/bin/env python3
import pandas as pd

# !!! The MWE does **not** reproduce the error because
# !!! I don't know how to generate `<M8[ns]` values.

df = pd.DataFrame({
    'ID': [1, 2],
    'A': [
        pd.Timestamp('2022-09-11 19:04:47'),
        pd.Timestamp('2022-09-11 11:57:39'),
    ]})
print(df.A.dtype)  # dtype('O')


x = pd.DataFrame({
    'ID': [2],
    'A': [
        pd.Timestamp('2016-09-11 00:01:22'),  # this should be of type "<M8[ns]"
    ]})

print(x.A.dtype)  # dtype('<M8[ns]')

df.update(x)
print(df)
0 Answers
Related