First Value in Pandas DatetimeIndex is Unsearchable

时间:2017-04-06 17:14:30

标签: python pandas

I have a simple Series:

>>> sub_dim_metrics
date
2017-04-04 00:00:00+00:00     32.38
2017-04-03 00:00:00+00:00    246.28
2017-04-02 00:00:00+00:00    146.25
2017-04-01 00:00:00+00:00    201.98
2017-03-31 00:00:00+00:00    274.74
2017-03-30 00:00:00+00:00    257.82
2017-03-29 00:00:00+00:00    279.38
2017-03-28 00:00:00+00:00    203.53
2017-03-27 00:00:00+00:00    250.65
2017-03-26 00:00:00+00:00    180.59
2017-03-25 00:00:00+00:00    196.61
2017-03-24 00:00:00+00:00    281.04
2017-03-23 00:00:00+00:00    276.44
2017-03-22 00:00:00+00:00    227.55
2017-03-21 00:00:00+00:00    267.59
Name: area, dtype: float64
>>> sub_dim_metrics.index
DatetimeIndex(['2017-04-04', '2017-04-03', '2017-04-02', '2017-04-01',
               '2017-03-31', '2017-03-30', '2017-03-29', '2017-03-28',
               '2017-03-27', '2017-03-26', '2017-03-25', '2017-03-24',
               '2017-03-23', '2017-03-22', '2017-03-21'],
              dtype='datetime64[ns, UTC]', name=u'date', freq=None)

Later in my code I retrieve the area for specific days using the following format: sub_dim_metrics['2017-04-02'], for example.

Before I retrieve area for a certain day, I first verify that the requested date is in the Series, like so: if '2017-04-02' in sub_dim_metrics.index

My problem is that the first value in the Index does not return true, while the rest do:

>>> '2017-04-02' in sub_dim_metrics.index
True
>>> '2017-04-04' in sub_dim_metrics.index
False

Why is this and what is the best way to verify a date is in my Series before retrieving its corresponding value?

2 个答案:

答案 0 :(得分:3)

IIUC:

您预计False时会收到True
您正在检查字符串是否在日期时间索引中。显然pandas对支票很松散,并试图为你做。它已经弄错了,不是吗。

计划1
做对了!

pd.to_datetime('2017-04-04') in sub_dim_metrics.index

True

计划2
我认为未分类的东西正在抛弃它。 sort_values首先。

'2017-04-04' in sub_dim_metrics.index.sort_values()

True

设置

from io import StringIO
import pandas as pd

txt = """2017-04-04 00:00:00+00:00     32.38
2017-04-03 00:00:00+00:00    246.28
2017-04-02 00:00:00+00:00    146.25
2017-04-01 00:00:00+00:00    201.98
2017-03-31 00:00:00+00:00    274.74
2017-03-30 00:00:00+00:00    257.82
2017-03-29 00:00:00+00:00    279.38
2017-03-28 00:00:00+00:00    203.53
2017-03-27 00:00:00+00:00    250.65
2017-03-26 00:00:00+00:00    180.59
2017-03-25 00:00:00+00:00    196.61
2017-03-24 00:00:00+00:00    281.04
2017-03-23 00:00:00+00:00    276.44
2017-03-22 00:00:00+00:00    227.55
2017-03-21 00:00:00+00:00    267.59"""

sub_dim_metrics = pd.read_csv(StringIO(txt),
    sep='\s{2,}', engine='python',
    index_col=0, parse_dates=[0],
    header=None, names=['date', 'area'],
    squeeze=True)

答案 1 :(得分:1)

public static String capitalize(String str, final String delimitersRegex) {
    if (str == null || str.length() == 0) {
        return "";
    }

    final Pattern delimPattern;
    if (delimitersRegex == null || delimitersRegex.length() == 0){
        delimPattern = Pattern.compile("\\W");
    }else {
        delimPattern = Pattern.compile(delimitersRegex);
    }

    final Matcher delimMatcher = delimPattern.matcher(str);
    boolean delimiterFound = delimMatcher.find();

    int delimeterStart = -1;
    if (delimiterFound){
        delimeterStart = delimMatcher.start();
    }

    final int strLen = str.length();
    final StringBuilder buffer = new StringBuilder(strLen);

    boolean capitalizeNext = true;
    for (int i = 0; i < strLen; i++) {
        if (delimiterFound && i == delimeterStart) {
            final int endIndex = delimMatcher.end();

            buffer.append( str.substring(i, endIndex) );
            i = endIndex;

            if( (delimiterFound = delimMatcher.find()) ){
                delimeterStart = delimMatcher.start();
            }

            capitalizeNext = true;
        } else {
            final char ch = str.charAt(i);

            if (capitalizeNext) {
                buffer.append(Character.toTitleCase(ch));
                capitalizeNext = false;
            } else {
                buffer.append(ch);
            }
        }
    }
    return buffer.toString();
}

密钥存在。

  len(s.get_value('2017-04-02)) == 0
  False 

密钥不存在。