Skip to content

How should I replace the NaN or None values for inserting timestamptz columns? #63

Answered by Brooke-white
ykparkwixon asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @ykparkwixon,

You'll want to replace NaN values with None. I've run the following example locally without issue

import redshift_connector
import pandas as pd
import numpy as np

df = pd.DataFrame([[np.nan],
                   [np.nan],
                   [np.nan],
                   [None]],
                  columns=list("a"))
df = df.replace({np.nan: None})

with redshift_connector.connect(...) as conn:
    with conn.cursor() as cursor:
        cursor.execute("create table test (c1 timestamptz)")
        cursor.write_dataframe(df, 'test')
        cursor.execute("select * from test")
        data = cursor.fetchall()
        print(data)
>>> ([None], [None], [None], [None])

reference

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Brooke-white
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants