【问题标题】:Why does pandas 2min bucket print NaN although all my row values are numbers (not NaN)?尽管我所有的行值都是数字(不是 NaN),为什么 pandas 2min bucket 会打印 NaN?
【发布时间】:2018-06-26 15:54:48
【问题描述】:

我知道在我的数据 response_bytes 列中没有 NaN 值,因为当我运行时:data[data.response_bytes.isna()].count() 结果为 0。

当我运行 2 min bucket mean 然后 head 我得到 NaN:

print(data.reset_index().set_index('time').resample('2min').mean().head())

                     index  identity  user  http_code  response_bytes  unknown
time                                                                          
2018-01-31 09:26:00    0.5       NaN   NaN      200.0           264.0      NaN
2018-01-31 09:28:00    NaN       NaN   NaN        NaN             NaN      NaN
2018-01-31 09:30:00    NaN       NaN   NaN        NaN             NaN      NaN
2018-01-31 09:32:00    NaN       NaN   NaN        NaN             NaN      NaN
2018-01-31 09:34:00    NaN       NaN   NaN        NaN             NaN      NaN

为什么响应字节时间分桶均值具有 NaN 值?

我想试验并了解时间分桶在 pandas 中的工作原理。所以我使用日志文件:http://www.cs.tufts.edu/comp/116/access.log 作为输入数据,然后将其加载到 pandas DataFrame 中,然后应用时间桶 2 分钟(这是我生命中的第一次)并运行 mean(),我没想到会看到任何response_bytes 列中的 NaN,因为所有值都不是 NaN。

这是我的完整代码:

import urllib.request
import pandas as pd
import re
from datetime import datetime
import pytz

pd.set_option('max_columns',10)

def parse_str(x):
    """
    Returns the string delimited by two characters.

    Example:
        `>>> parse_str('[my string]')`
        `'my string'`
    """
    return x[1:-1]

def parse_datetime(x):
    '''
    Parses datetime with timezone formatted as:
        `[day/month/year:hour:minute:second zone]`

    Example:
        `>>> parse_datetime('13/Nov/2015:11:45:42 +0000')`
        `datetime.datetime(2015, 11, 3, 11, 45, 4, tzinfo=<UTC>)`

    Due to problems parsing the timezone (`%z`) with `datetime.strptime`, the
    timezone will be obtained using the `pytz` library.
    '''
    dt = datetime.strptime(x[1:-7], '%d/%b/%Y:%H:%M:%S')
    dt_tz = int(x[-6:-3])*60+int(x[-3:-1])
    return dt.replace(tzinfo=pytz.FixedOffset(dt_tz))

# data = pd.read_csv(StringIO(accesslog))
url = "http://www.cs.tufts.edu/comp/116/access.log"
accesslog =  urllib.request.urlopen(url).read().decode('utf-8')
fields = ['host', 'identity', 'user', 'time_part1', 'time_part2', 'cmd_path_proto', 
          'http_code', 'response_bytes', 'referer', 'user_agent', 'unknown']

data = pd.read_csv(url, sep=' ', header=None, names=fields, na_values=['-'])

# Panda's parser mistakenly splits the date into two columns, so we must concatenate them
time = data.time_part1 + data.time_part2
time_trimmed = time.map(lambda s: re.split('[-+]', s.strip('[]'))[0]) # Drop the timezone for simplicity
data['time'] = pd.to_datetime(time_trimmed, format='%d/%b/%Y:%H:%M:%S')

data.head()

print(data.reset_index().set_index('time').resample('2min').mean().head())

我期望 response_bytes 列的平均值的时间分段不是 NaN。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这是预期行为,因为resampling 转换为固定时间间隔,因此如果没有样本,您将获得NaN

    所以这意味着在大约 2 分钟的迭代之间没有日期时间,例如2018-01-31 09:28:002018-01-31 09:30:00,所以mean 不能被计数并得到NaNs。

    print (data[data['time'].between('2018-01-31 09:28:00','2018-01-31 09:30:00')])
    Empty DataFrame
    Columns: [host, identity, user, time_part1, time_part2, cmd_path_proto,
              http_code, response_bytes, referer, user_agent, unknown, time]
    Index: []
    
    [0 rows x 12 columns]
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2013-07-01
      • 2022-01-20
      • 2016-01-13
      • 1970-01-01
      • 2021-12-18
      • 2014-05-04
      • 1970-01-01
      相关资源
      最近更新 更多