2016-06-30 4 views
0

私は毎日テーブルに追加する準備をしているダニデータファイルを計算しています。最終的なDataFrameには、ティッカー、平均スプレッド、最大スプレッド、および日付が表示されます。空の状態になっている日付の列を除いて、他のすべては正常に動作します。私の日付の列が空白になっているのはなぜですか?

チックデータ自体には、時刻をフォーマットで示す「タイムスタンプ」という名前の列があります。 2016-06-03T14:27:16.548084-4:00。 私は日付(2016-06-03)のみが必要です。各ファイルは1日分のため、このスクリプトを実行する各ファイルの各行で同じにする必要があります。時間だけが異なります。

最終的な結果は次のようになります。

a | 0.22 | 1.84 | 2016-06-03 
aa | 0.01 | 0.10 | 2016-06-03 
aaap | 2.07 | 2.17 | 2016-06-03 
aal | 0.15 | 0.5 | 2016-06-03 

私は同じ結果、空の日付列でDTYPE strと同様とdf2['date'] = df['timestamp'].head(1) * len(df2.index)を使用してみました。どこが間違っていますか?

import pandas as pd 
import numpy as np 
from datetime import datetime 


df = pd.read_csv('C:\\Users\\tickdata.csv', 
       dtype={'ticker': str, 'timestamp': datetime, 'bidPrice': np.float32, 'askPrice': np.float32, 'afterHours': str}, 
       usecols=['ticker', 'timestamp', 'bidPrice', 'askPrice', 'afterHours'] 
       ) 

#afterhours and single sided quotes need to be filtered out 
#create the spread column to analyze 
df = df[df.afterHours == "False"] 
df = df[df.bidPrice != 0] 
df = df[df.askPrice != 0] 
df['spread'] = (df.askPrice - df.bidPrice) 

#compute the average and max to a seperate DataFrame 
#grab the date from the first row 
df2 = pd.DataFrame() 
df2['avg_spread'] = df.groupby(['ticker'])['spread'].mean() 
df2['maximum'] = df.groupby(['ticker'])['spread'].max() 
df2['date'] = df['timestamp'].head(1) 

UPDATE:

import pandas as pd 
import numpy as np 
import psycopg2 as pg 
import datetime as dt 


df = pd.read_csv('C:\\Users\\tickdata.csv', 
       dtype={'ticker': str, 'timestamp': str, 'bidPrice': np.float32, 'askPrice': np.float32, 'afterHours': str}, 
       usecols=['ticker', 'timestamp', 'bidPrice', 'askPrice', 'afterHours'], 
       ) 
#afterhours and single sided quotes need to be filtered out 
#create the spread column to analyze 
df = df[df.afterHours == "False"] 
df = df[df.bidPrice != 0] 
df = df[df.askPrice != 0] 
df['spread'] = (df.askPrice - df.bidPrice) 

#convert timestamp to date 
df['timestamp'] = pd.to_datetime(df['timestamp']) 
df['date'] = df.timestamp.dt.date 

#compute the average and max to a seperate DataFrame 
#grab the date from the first row 
df2 = pd.DataFrame() 
df2['avg_spread'] = df.groupby(['ticker'])['spread'].mean() 
df2['maximum'] = df.groupby(['ticker'])['spread'].max() 
df2['date'] = df.groupby(['ticker'])['date'] 

今すぐDF2に表示される日付を取得する方法を把握しよう。日付文字列を取得する日時及びdt.dateに変換する df2['date'] = df.groupby(['ticker'])['date'].first()

答えて

0

使用to_datetimeを使用するために必要なdf2['date'] = df.groupby(['ticker'])['date']

df2['date'] = df['date'] 

UPDATE 2 [解決しよう] を試してみました。

df['timestamp'] = pd.to_datetime(df['timestamp']) 
df['date'] = df.timestamp.dt.date 
+0

ありがとうございます!私は今、日付を分けました。フォローアップの質問:最終的なDataFrame(df2)については、どのように 'date'列を取得するのですか?それは元のDataFrame(df)でうまく表示されますが、df2に表示されるように見えず、groupbyを使ってカラムと何か関係があると思います。しかし、df2 ['date'] = df.groupby(['ticker'])['date']は動作しません。また、df2 ['date'] = df ['date' ]。 OPで更新されたコード! – William

+0

集計しようとするものに応じて、最初の 'df.groupby(['ticker'])['date']。first()'のようなagg関数を使用する必要があります – ksindi

+0

ティッカーでグループ化したいと思うもの – ksindi

関連する問題