Home > OS >  How to convert Pandas Series to Timestamp when not every value is convertible?
How to convert Pandas Series to Timestamp when not every value is convertible?

Time:01-08

Context

I have a Pandas Series containing Dates in a String format (e.g. 2017-12-19 09:35:00). My goal is to convert this Series into Timestamps (Time in Seconds since 1970).

The difficulty is, that some Values in this Series are corrupt and cannot be converted to a Timestamp. In that case, they should be converted to None.


Code

import datetime

series = series.apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S").timestamp())

Question

The code above would work when all Values are in the correct format, however there is corrupt data.

  • How can I achieve my goal while converting all not-convertible data to None?

CodePudding user response:

Create a function with try except, like this:

def to_timestamp(x):
    try:
        return datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S").timestamp()
    except:
        return None

series = series.apply(to_timestamp)

CodePudding user response:

Pandas typically represents invalid timestamps with NaT (Not a Time). You can use pd.to_datetime with errors="coerce":

import pandas as pd

series = pd.Series(["2023-01-07 12:34:56", "error"])
out = pd.to_datetime(series, format="%Y-%m-%d %H:%M:%S", errors="coerce")

output:

0   2023-01-07 12:34:56
1                   NaT
dtype: datetime64[ns]
  • Related