1

When I create new DataFrame with custom index values it replaces data from columns with NaN/NaT values.

I've tried assigning pd.Index as an index argument as well, and result was the same.

pd.DataFrame(
    data={
        "date": pd.Series([datetime(2000, 1, 2)]),
        "duration": pd.Series([datetime(1970, 1, 1, 0, 5)]),
    }
    index = [1] 
    )

Instead of:

    date    duration
1   2000-01-02  1970-01-01 00:05:00

I receive:

    date    duration
1   NaT NaT

Is it a bug?

I use pandas 0.25.0

1
  • The indexes in your dataframe constructor and in your series have to match. pd.Series yields default indexes starting at 0, and you passed [1] as argument in your pd.DataFrame. Commented Sep 16, 2019 at 13:28

1 Answer 1

1

Remove Series from DataFrame constructor, because their default index is 0, so different like index of DataFrame, so returned missing values (indices have to match):

df = pd.DataFrame(
    data={
        "date": [datetime(2000, 1, 2)],
        "duration": [datetime(1970, 1, 1, 0, 5)],
    },
    index = [1] 
    )

print (df)
        date            duration
1 2000-01-02 1970-01-01 00:05:00

Detail:

print (pd.Series([datetime(2000, 1, 2)]))
0   2000-01-02
dtype: datetime64[ns]

So if need Series, there is necessary set index to 1 too:

df = pd.DataFrame(
    data={
        "date": pd.Series([datetime(2000, 1, 2)], index = [1]),
        "duration": pd.Series([datetime(1970, 1, 1, 0, 5)],index = [1]),
    },
    index = [1] 
    )

Or remove in DataFrame for default 0 index:

df = pd.DataFrame(
    data={
        "date": pd.Series([datetime(2000, 1, 2)]),
        "duration": pd.Series([datetime(1970, 1, 1, 0, 5)]),
    },
    )

print (df)
        date            duration
0 2000-01-02 1970-01-01 00:05:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.