<p>我有一个很长的时间序列</p>
<pre><code>import pandas as pd
index=pd.date_range(start='2012-11-05', end='2012-11-10', freq='1S').tz_localize('Europe/Berlin')
df=pd.DataFrame(range(len(index)), index=index, columns=['Number'])
</code></pre>
<p>现在我要提取每天的所有子数据帧,以获得以下输出:</p>
<pre><code>df_2012-11-05: data frame with all data referring to day 2012-11-05
df_2012-11-06: etc.
df_2012-11-07
df_2012-11-08
df_2012-11-09
df_2012-11-10
</code></pre>
<p>最有效的方法是避免检查index.date==give_date是否非常慢。另外,用户不知道一个修道院在帧中的天数范围。</p>
<p>有什么提示是用迭代器来实现的吗?</p>
<p>我目前的解决方案是这样的,但并不那么优雅,有两个问题定义如下:</p>
<pre><code>time_zone='Europe/Berlin'
# find all days
a=np.unique(df.index.date) # this can take a lot of time
a.sort()
results=[]
for i in range(len(a)-1):
day_now=pd.Timestamp(a[i]).tz_localize(time_zone)
day_next=pd.Timestamp(a[i+1]).tz_localize(time_zone)
results.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(df[day_now:day_next]) # how to select if I do not want day_next included?
# last day
results.append(df[day_next:])
</code></pre>
<p>这种方法存在以下问题:</p>
<ul>
<li>a=np.unique(df.index.date)可能需要很多时间</li>
<li>df[day_now:day_next]包括day_next,但我需要将其排除在范围内</li>
</ul>
<p>如果要按日期(即:年+月+日)分组,请使用<code>df.index.date</code>:</p>
<pre><code>result = [group[1] for group in df.groupby(df.index.date)]
</code></pre>
<p>因为<code>df.index.day</code>将使用月份的日期(即从1到31)进行分组,如果输入的数据帧日期延长到多个月,则可能会导致不良行为。</p>