Code Dogs Technical Documentation
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Pandas & Numpy data wrangling

Imports

import pandas as pd
import numpy as np 

Pandas

Unix timestamp to a python datetime object, and set as the index:

df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df = df.set_index('timestamp')

And from a datetime object to a unix timestamp:

df.index.astype(np.int64) // 10**6

Multiple columns to a numeric type:

for (column_name, _) in df.items():
    if column_name == 'timestamp' or column_name == 'some-column-that-contains-strings':
        continue
    df[column_name] = pd.to_numeric(df[column_name], errors='coerce')