DateTimeIndex values are assigned across entire df when using .loc · Issue #9478 · pandas-dev/pandas (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

@alan-wong

Description

@alan-wong

I posted a 2-part answer to this question on SO: http://stackoverflow.com/questions/28482553/pandas-set-value-of-column-to-value-of-index-based-on-condtion

What I noticed is that if your index is a datetimeindex then assigning the values is not respecting the column selection and is blatting all rows.

I am using pandas 0.15.2 using numpy 1.9.1 and python 3.4 64-bit

example:

In [46]:

rows = 3
df = pd.DataFrame(np.random.randn(rows,2), columns=list('AB'), index=pd.date_range('1/1/2000', periods=rows, freq='1H'))
print(df)
df.loc[df.A > 0.5, 'LAST_TIME_A_ABOVE_X'] = df.loc[df.A > 0.5].index
df
                            A         B
2000-01-01 00:00:00 -0.761643  0.969167
2000-01-01 01:00:00  0.050335 -1.346953
2000-01-01 02:00:00  0.663857 -0.272247
Out[46]:
                             A                             B  \
2000-01-01 00:00:00 1970-01-01           1970-01-01 00:00:00   
2000-01-01 01:00:00 1970-01-01 1969-12-31 23:59:59.999999999   
2000-01-01 02:00:00 1970-01-01           1970-01-01 00:00:00   

                    LAST_TIME_A_ABOVE_X  
2000-01-01 00:00:00                 NaT  
2000-01-01 01:00:00                 NaT  
2000-01-01 02:00:00 2000-01-01 02:00:00 

If the index is an Int64 type then this doesn't happen.

If you reset the index and then assign the values using .loc then it works correctly