red79phoenix
red79phoenix

Reputation: 91

Convert pandas unicode column to timestamp python

I am getting:

TypeError: Cannot compare type 'Timestamp' with type 'unicode'

When I try to do this:

df = df[df['timestamp'] >= start]
df = df[df['timestamp'] < (end + timedelta(days=1))]

The data types are:

type(df['timestamp'][0])
Out[134]: unicode
type(start)
Out[135]: pandas._libs.tslib.Timestamp

So, I have been trying to convert the column to a pandas datetime. I do the following and it is still a unicode data type.

df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%m:%s', errors='coerce')

Upvotes: 1

Views: 407

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477318

The error says:

TypeError: Cannot compare type 'Timestamp' with type 'unicode'

So that means you made a comparison, with Timestamp on the left side, and unicode on the right side (given the error occurs of course at the comparison level).

Since the error occurs at:

df = df[df['timestamp'] >= start]

This thus means that start is a unicode string, not a timestamp.

You thus first need to convert start, for example with:

df = df[df['timestamp'] >= pd.to_datetime(start)]

Upvotes: 1

Related Questions