[Python] Is pandas.read_spss misreading datetime into unix?

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a sav file with a datetime column in %m/%d/%Y string format. When I read it in with pd.read_spss(), which doesn't seem to have any datetime-related arguments, it ends up in what looks like unix time, except that the time would be a few centuries from now with unique values including 13778726400, 13841884800, etc.

    When I feed the read column into pd.to_datetime, however, it's not interpreted as the date I would expect, but rather a few seconds after the original unix date in 1970:

    pd.to_datetime(df.col)

    0 1970-01-01 00:00:13.778726400
    1 1970-01-01 00:00:13.841798400
    2 1970-01-01 00:00:13.778726400
    3 1970-01-01 00:00:13.778035200
    4 1970-01-01 00:00:13.841798400


    Why is the datetime column getting read in in this weird format, and why is pd.to_datetime not able to convert it back?

    (My current workaround is just to set date columns to string manually in SPSS. Then pyreadstat/pandas.read_spss has no trouble reading it as a string and pandas.to_dateime can transform.)

    Continue reading...

Compartilhe esta Página