python-pandas:在pandas dataframe

时间:2016-08-07 07:41:03

标签: python datetime pandas

我有一个带有混合数据类型列的数据框,我应用了pd.to_datetime(df['DATE'],coerce=True)并获得了以下数据框

CUSTOMER_name     DATE
 abc                 NaT
 def                 NaT
 abc               2010-04-15 19:09:08
 def               2011-01-25 15:29:37
 abc               2010-04-10 12:29:02

现在我想应用一些agg函数(这里我想组合使用mailid并使用Date的min()来查找mailid的第一笔交易日期。)

df['DATE'] = [x.date() for x in df['DATE']]
#Here the value goes to 
 CUSTOMER_name     DATE
 abc               0001-255-255 ####how??
 def               0001-255-255  ###How??
 abc               2010-04-15
 def               2011-01-25
 abc               2010-04-10
#Then when i do a groupby and applying min on DATE
df.groupby('CUSTOMER_name')['DATE'].min()
#CUSTOMER_name     DATE
 abc               0001-255-255 ####i want 2010-04-10
 def               0001-255-255  ### i want 2011-01-25

所以任何人都可以建议,如何在转换为date()时处理此NaT,并在执行groupby和min()时,如何排除NaT进行计算。

如果对于任何customer_name,只有NaT将在DATE字段中存在,那么在groupby和min()上,我可以使用nan或Null值。

2 个答案:

答案 0 :(得分:2)

假设你从这样的事情开始:

name 'w' is not defined
{}

(请注意,唯一的区别是添加映射到df = pd.DataFrame({ 'CUSTOMER_name': ['abc', 'def', 'abc', 'def', 'abc', 'fff'], 'DATE': ['NaT', 'NaT', '2010-04-15 19:09:08', '2011-01-25 15:29:37', '2010-04-10 12:29:02', 'NaT']}) df.DATE = pd.to_datetime(df.DATE) 的{​​{1}}。

然后你会问以下问题:

fff

这是因为NaT - >>> pd.to_datetime(df.DATE.groupby(df.CUSTOMER_name).min()) CUSTOMER_name abc 2010-04-10 12:29:02 def 2011-01-25 15:29:37 fff NaT Name: DATE, dtype: datetime64[ns] 已经在适用的情况下排除了丢失的数据(虽然更改了结果的格式),最终groupby再次强制将结果强制转换为{{1} }}

要获取结果的日期部分(我认为这是一个单独的问题),请使用min

pd.to_datetime

答案 1 :(得分:1)

这是另一种解决方案:

数据:

In [96]: x
Out[96]:
  CUSTOMER_name                 DATE
0           abc                    T
1           def                    N
2           abc  2010-04-15 19:09:08
3           def  2011-01-25 15:29:37
4           abc  2010-04-10 12:29:02
5           fff                   sa

<强>解决方案:

In [100]: (x.assign(D=pd.to_datetime(x.DATE, errors='coerce').values.astype('<M8[D]'))
   .....:   .groupby('CUSTOMER_name')['D']
   .....:   .min()
   .....:   .astype('datetime64[ns]')
   .....: )
Out[100]:
CUSTOMER_name
abc   2010-04-10
def   2011-01-25
fff          NaT
Name: D, dtype: datetime64[ns]

<强>解释

首先,让我们创建一个带有截断时间部分的新虚拟列D

In [97]: x.assign(D=pd.to_datetime(x.DATE, errors='coerce').values.astype('<M8[D]'))
Out[97]:
  CUSTOMER_name                 DATE          D
0           abc                    T        NaT
1           def                    N        NaT
2           abc  2010-04-15 19:09:08 2010-04-15
3           def  2011-01-25 15:29:37 2011-01-25
4           abc  2010-04-10 12:29:02 2010-04-10
5           fff                   sa        NaT

现在我们可以按CUSTOMER_name进行分组,并为每个组计算最低D

In [101]: x.assign(D=pd.to_datetime(x.DATE, errors='coerce').values.astype('<M8[D]')).groupby('CUSTOMER_name')['D'].min()
Out[101]:
CUSTOMER_name
abc    1.270858e+18
def    1.295914e+18
fff             NaN
Name: D, dtype: float64

最后将结果列转换为datetime64[ns] dtype:

In [102]: (x.assign(D=pd.to_datetime(x.DATE, errors='coerce').values.astype('<M8[D]'))
   .....:   .groupby('CUSTOMER_name')['D']
   .....:   .min()
   .....:   .astype('datetime64[ns]')
   .....: )
Out[102]:
CUSTOMER_name
abc   2010-04-10
def   2011-01-25
fff          NaT
Name: D, dtype: datetime64[ns]