3

Pythonでは、500.000行で次のようなデータがあります:

タイムカウント

1-1-1900 10:41:00 1

3-1-1900 09:54:00 1

4-1-1900 15:45:00 1

5-1-1900 18:41:00 1

4-1-1900 15:45:00 1

そして、次のような四分の一のビンで新しい列を作成したいと思います:

ビン数

9:00~9:15 2

9:15~9:30 4

9:30~9:45 4

10:00~10:15 4

ビンの作成方法は知っていますが、タイムスタンプが問題になります。誰かがこれで私を助けることができますか? もうありがとう!

4

3 に答える 3

1

うーん、これがあなたが求めたものかどうかはわかりません。そうでない場合は、問題を理解するのが非常に難しいため、質問を改善することをお勧めします。特に、あなたがすでにやろうとしていることを見るといいでしょう。

from __future__ import division, print_function
from collections import namedtuple
from itertools import product
from datetime import time
from StringIO import StringIO


MAX_HOURS = 23
MAX_MINUTES = 59


def process_data_file(data_file):
    """
    The data_file is supposed to be an opened file object
    """
    time_entry = namedtuple("time_entry", ["time", "count"])
    data_to_bin = []
    for line in data_file:
        t, count = line.rstrip().split("\t")
        t = map(int, t.split()[-1].split(":")[:2])
        data_to_bin.append(time_entry(time(*t), int(count)))
    return data_to_bin


def make_milestones(min_hour=0, max_hour=MAX_HOURS, interval=15):
    minutes = [minutes for minutes in xrange(MAX_MINUTES+1) if not minutes % interval]
    hours = range(min_hour, max_hour+1)
    return [time(*milestone) for milestone in list(product(hours, minutes))]


def bin_time(data_to_bin, milestones):
    time_entry = namedtuple("time_entry", ["time", "count"])
    data_to_bin = sorted(data_to_bin, key=lambda time_entry: time_entry.time, reverse=True)
    binned_data = []
    current_count = 0
    upper = milestones.pop()
    lower = milestones.pop()
    for entry in data_to_bin:
        while not lower <= entry.time <= upper:
            if current_count:
                binned_data.append(time_entry("{}-{}".format(str(lower)[:-3], str(upper)[:-3]), current_count))
                current_count = 0
            upper, lower = lower, milestones.pop()
        current_count += entry.count
    return binned_data


data_file = StringIO("""1-1-1900 10:41:00\t1
3-1-1900 09:54:00\t1
4-1-1900 15:45:00\t1
5-1-1900 18:41:00\t1
4-1-1900 15:45:00\t1""")


binned_time = bin_time(process_data_file(data_file), make_milestones())
for entry in binned_time:
    print(entry.time, entry.count, sep="\t")

出力:

18:30-18:45 1
15:45-16:00 2
10:30-10:45 1
于 2015-05-10T22:29:32.627 に答える