2017-05-25 4 views
0

Pandas ist eine meiner Spalten beim Resampling fallen, und ich verstehe nicht warum. Ich habe gelesen, dass dies geschehen kann, wenn die Spalten, keinen richtigen Zahlentyp haben, aber das ist hier nicht der Fall:Pandas resample Tropfen Spalte

import pandas; 

# movements is the target data frame with daily movements 
movements = pandas.DataFrame(columns=['date', 'amount', 'cash']); 
movements.set_index('date', inplace=True); 

# df is a movement to add 
df = pandas.DataFrame({'amount': 179, 
         'cash': 100.00}, 
         index=[pandas.Timestamp('2015/12/31')]); 
print(df); print(df.info()); print(); 

# add df to movements and resample movements 
movements = movements.append(df).resample('D').sum().fillna(0); 
print(movements); print(movements.info()); 

Ergebnisse in:

  amount cash 
2015-12-31  179 100.0 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 1 entries, 2015-12-31 to 2015-12-31 
Data columns (total 2 columns): 
amount 1 non-null int64 
cash  1 non-null float64 
dtypes: float64(1), int64(1) 
memory usage: 24.0 bytes 
None 

      cash 
2015-12-31 100.0 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 1 entries, 2015-12-31 to 2015-12-31 
Freq: D 
Data columns (total 1 columns): 
cash 1 non-null float64 
dtypes: float64(1) 
memory usage: 16.0 bytes 
None 

Ich bemerkte, dass die drop geschieht nur, wenn cash ein float ist, dh wenn in dem Code über cash auf 100 (int) statt 100.00 gesetzt ist, dann sind alle Spalten int und amount wird nicht gelöscht.

Irgendeine Idee?

Antwort

1

Das Problem ist, wenn Sie Bewegungen DF erstellt haben, wird der Datentyp für die Spalten auf Objekt gesetzt.

Wenn Sie die Spaltentypen im Voraus festlegen oder später in numerische Typen ändern, sollte es funktionieren.

movements.append(df).apply(pd.to_numeric).resample('D').sum().fillna(0) 
Out[100]: 
      amount cash 
2015-12-31  179 100.0 
+0

Vielen Dank! Für andere, die das gleiche Problem haben, habe ich mich entschieden, es explizit mit 'movements = movements.astype (numpy.float);'. – Xavier

Verwandte Themen