Pandas,list.append或Google AnalyticsAPI在Python中提供了错误的数据格式或响应

时间:2014-04-10 19:35:14

标签: python pandas append google-analytics-api

我有一个包含日期范围的元组列表,如下所示:

[('2012-01-01', '2012-01-25'),
 ('2012-01-26', '2012-02-20'),
 ('2012-02-21', '2012-03-16'),
 ('2012-03-17', '2012-04-11'),
 ('2012-04-12', '2012-05-06')...]

def pull_unsampled(date_list):

    df123 = []

    for start, end in date_list:

        df123.append(ga.read_ga(metrics, 
                    dimensions = "yearMonth", 
                    start_date = start,
                    end_date = end, 
                    account_id = account_id, profile_id = profile_id))
        print start, end

    return df123

pull_unsampled(unsampled_date_ranges)

在上面的功能中,我获取元组列表,并通过Pandas将开始日期和结束日期传递给Google AnalyticsAPI请求。我打印了通过for循环的开始和结束日期,并打印出它们在原始列表中的显示方式。当我收到回复数据时,我会收到没有像输入那样格式化的日期,事实上我给了不同的日期。

[                                           visits  transactionsPerVisit  \
deviceCategory operatingSystem yearMonth                                  
desktop        (not set)       2020-12-01     137              0.000000   
               Chrome OS       2020-12-01      20              0.000000   
               Google TV       2020-12-01       5              0.000000   
               Linux           2020-12-01     363              0.275482   
               Macintosh       2020-12-01  111467              0.909686   
               Nintendo Wii    2020-12-01       2              0.000000   
               Playstation 3   2020-12-01       1              0.000000   
               SunOS           2020-12-01       3              0.000000   
               Windows         2020-12-01   98147              0.996464   
mobile         Android         2020-12-01    2976              0.302419   
               BlackBerry      2020-12-01    1051              0.000000   
               Nokia           2020-12-01       2              0.000000   
               Samsung         2020-12-01       1              0.000000   
               SymbianOS       2020-12-01       4              0.000000   
               Windows Phone   2020-12-01      46              0.000000   
               iPad            2020-12-01       2              0.000000   
               iPhone          2020-12-01   24754              0.379737   
               iPod            2020-12-01     331              0.000000   
tablet         Android         2020-12-01     189              0.529101   
               iPad            2020-12-01   24973              0.828895  

由于某种原因,('2012-01-01', '2012-01-25')的日期范围会返回上面yearMonth列的不正确日期,但后来的响应会正确显示。例如,('2014-03-06', '2014-03-31')的最后一个日期范围提取正确显示yearMonth:

                                          avgTimeOnSite  pageviewsPerVisit  
deviceCategory operatingSystem yearMonth                                    
desktop        (not set)       201403          1.127708           1.013683  
               Chrome OS       201403        159.150562           3.811236  
               Google TV       201403          0.000000           1.000000  
               Linux           201403        431.220165           8.705575  
               Macintosh       201403        219.625070           5.193008  
               Nintendo Wii    201403          0.000000           1.000000  
               Playstation 3   201403         65.000000           3.000000  
               Windows         201403        186.650353           4.077768  
               Xbox            201403          0.000000           1.000000  
mobile         (not set)       201403         51.151515           2.212121  
               Android         201403        143.776396           3.195142  
               BlackBerry      201403        132.644037           3.049541  
               Nokia           201403          0.400000           1.000000  
               Series40        201403          5.071429           1.214286  
               SymbianOS       201403          0.000000           1.000000  
               Windows Phone   201403        172.685446           3.305164  
               iOS             201403        126.005593           2.954413  
tablet         Android         201403        183.609062           3.746048  
               BlackBerry      201403        224.421053           3.789474  
               iOS             201403        217.915084           4.462462 

这是list.append,pandas还是Google AnalyticsAPI中的错误?我的代码中是否有一些东西可以防止这些奇怪的日期代替实际的日期?

编辑:

这是一个简单的调用,它返回相同日期范围内的yearMonth数据:

df = ga.read_ga(metrics, 
            dimensions = dimensions, 
            start_date = start_date,
            end_date = end_date, 
            account_id = account_id, profile_id = profile_id)

df

返回为:

    visits
yearMonth   
201001   270094
201002   254034
201003   305295
201004   311824
201005   281570
201006   249348
201007   247005
201008   245882
201009   252296
201010   266417
201011   274349
201012   272913
201101   259959
201102   261080
201103

0 个答案:

没有答案