簡體   English   中英

熊貓 - 巨大的內存消耗

[英]Pandas - Huge memory consumption

從一個大約有1500萬行(大約250 MB)的pickle加載一個數據幀后,我對它執行了一些搜索操作,然后刪除了一些行。 在這些操作期間,內存使用量猛增至5,有時甚至是7 GB,由於交換而煩人(我的筆記本電腦只有8 GB內存)。

關鍵是當操作完成時(即,執行下面代碼中的最后兩行時)不釋放該存儲器。 所以Python進程仍然需要7 GB的內存。

知道為什么會這樣嗎? 我正在使用Pandas 0.20.3。

下面的最小例子。 實際上'數據'變量有大約1500萬行,但我不知道如何在這里發布。

import datetime, pandas as pd

data = {'Time':['2013-10-29 00:00:00', '2013-10-29 00:00:08', '2013-11-14 00:00:00'], 'Watts': [0, 48, 0]}
df = pd.DataFrame(data, columns = ['Time', 'Watts'])
# Convert string to datetime
df['Time'] = pd.to_datetime(df['Time'])
# Make column Time as the index of the dataframe
df.index = df['Time']
# Delete the column time
df = df.drop('Time', 1)

# Get the difference in time between two consecutive data points
differences = df.index.to_series().diff()
# Keep only the differences > 60 mins
differences = differences[differences > datetime.timedelta(minutes=60)]
# Get the string of the day of the data points when the data gathering resumed
toRemove = [datetime.datetime.strftime(date, '%Y-%m-%d') for date in differences.index.date]

# Remove data points belonging to the day where the differences was > 60 mins
for dataPoint in toRemove:
    df.drop(df[dataPoint].index, inplace=True)

您可能想嘗試調用垃圾收集器。 gc.collect()請參閱如何在Python中顯式釋放內存? 欲獲得更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM