2018-10-22

class Asset(object):

    share=0

    from datetime import datetime

    buyTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    sellTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    def __init__(self,id,price):

        self.id=id

        self.price=price

    def buy(self,share,time):

        self.share+=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

    def sell(self,share,time):

        self.share-=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

class NonShortableAsset(Asset):

    def sell(self,share,time):

        if self.share>=share:

            self.share-=share

            self.sellTime=datetime.strptime(time,"%Y-%m-%d")

        else:

            print("not permitted!There are not enough share to sell!")

            print("Current share is %s"%self.share)

            return

arrange、list切片 首包含,尾不包含,linspace 首尾都包含,list、array 索引从0开始


a=float(input("please input a num"))

if a is not int:

    print("ERROR,Please input an integer")

elif (a%2)!=0:

    print(a)

else:

    print(0)

import random

for i in range(5):

    a=random.randint(-1000,1000)

    print(a)

    if a>=0:

        print("Big")

    else:

        print("Small")


class Asset(object):

    share=0

    from datetime import datetime

    buyTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    sellTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    def __init__(self,id,price):

        self.id=id

        self.price=price

    def buy(self,share,time):

        self.share+=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

    def sell(self,share,time):

        self.share-=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

class NonShortableAsset(Asset):

    def sell(self,share,time):

        if self.share>=share:

            self.share-=share

            self.sellTime=datetime.strptime(time,"%Y-%m-%d")

        else:

            print("not permitted!There are not enough share to sell!")

            print("Current share is %s"%self.share)

            return

def OpenPrice(priceSequence):

    Open=priceSequence[0]

    return Open

from datetime import datetime

import pandas as pd

date=datetime(2016,1,1)

date=pd.Timestamp(date)

print(date)

ts=pd.Series(1,index=[date])

print(ts)

dates=[datetime(2016,1,1),datetime(2016,1,2),datetime(2016,1,3)]

ts=pd.Series([1,2,3],index=dates)

print(ts.index[0])

print(ts.truncate(after="2016-01-02"))

print(ts)

print(ts.shift(1))

Price=pd.Series([20.34,20.56,21.01,20.55,21.34],index=pd.to_datetime(["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05"]))

Roe=(Price-Price.shift(1))/Price.shift(1)

print(Roe)

rts=ts.resample("M",how="mean")

print(rts)

dates=["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05","2016-01-06"]

dates=pd.to_datetime(dates)

df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=list("ABCD"))

print(df.info())

print(df.head(3))

print(df.tail(4))

print(df.columns)

print(df.index)

print(df.values)

print(df.describe())

print(df[1:3])

print(df["A"])

print(df["A"]>0)

print(df.loc[:,"A"])

print(df.loc[:,"A":"C"])

print(df.loc[dates[0:2],"A":"C"])

print(df)

print(df.iloc[2])#第3行的数据,起始为0

print(df.ix[2:5])

print(df.T)


a=float(input("please input a num"))

if a is not int:

    print("ERROR,Please input an integer")

elif (a%2)!=0:

    print(a)

else:

    print(0)

import random

for i in range(5):

    a=random.randint(-1000,1000)

    print(a)

    if a>=0:

        print("Big")

    else:

        print("Small")


class Asset(object):

    share=0

    from datetime import datetime

    buyTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    sellTime=datetime.strptime("1990-01-01","%Y-%m-%d")

    def __init__(self,id,price):

        self.id=id

        self.price=price

    def buy(self,share,time):

        self.share+=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

    def sell(self,share,time):

        self.share-=share

        self.sellTime=datetime.strptime(time,"%Y-%m-%d")

class NonShortableAsset(Asset):

    def sell(self,share,time):

        if self.share>=share:

            self.share-=share

            self.sellTime=datetime.strptime(time,"%Y-%m-%d")

        else:

            print("not permitted!There are not enough share to sell!")

            print("Current share is %s"%self.share)

            return

def OpenPrice(priceSequence):

    Open=priceSequence[0]

    return Open

from datetime import datetime

import pandas as pd

date=datetime(2016,1,1)

date=pd.Timestamp(date)

print(date)

ts=pd.Series(1,index=[date])

print(ts)

dates=[datetime(2016,1,1),datetime(2016,1,2),datetime(2016,1,3)]

ts=pd.Series([1,2,3],index=dates)

print(ts.index[0])

print(ts.truncate(after="2016-01-02"))

print(ts)

print(ts.shift(1))

Price=pd.Series([20.34,20.56,21.01,20.55,21.34],index=pd.to_datetime(["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05"]))

Roe=(Price-Price.shift(1))/Price.shift(1)

print(Roe)

rts=ts.resample("M",how="mean")

print(rts)

dates=["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05","2016-01-06"]

dates=pd.to_datetime(dates)

df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=list("ABCD"))

print(df.info())

print(df.head(3))

print(df.tail(4))

print(df.columns)

print(df.index)

print(df.values)

print(df.describe())

print(df[1:3])

print(df["A"])

print(df["A"]>0)

print(df.loc[:,"A"])

print(df.loc[:,"A":"C"])

print(df.loc[dates[0:2],"A":"C"])

print(df)

print(df.iloc[2])#第3行的数据,起始为0

print(df.ix[2:5])

print(df.T)

dates=["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05","2016-01-06"]

dates=pd.to_datetime(dates)

df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=list("ABCD"))

# =============================================================================

# print(df)

# print(df.T)

# =============================================================================

df.sort_values(axis=0,by="A",ascending=False)

df.sort_values(axis=1,by="2016-01-02",ascending=False)

df=df.rank(axis=0)

print(df)

dates=["2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05","2016-01-06"]

dates=pd.to_datetime(dates)

df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=list("ABCD"))

s1=pd.Series([1,2,3,4,5,6],index=pd.date_range("20160101",periods=6,freq="D"))

df["E"]=s1

df=pd.concat([df,s1],axis=1)

df1=pd.DataFrame({"A":[1,2,3],"B":[4,5,6],"C":[7,8,9]},index=pd.date_range("20160110",periods=3))

df.append(df1)

pd.concat([df,df1],join="inner")

df.drop("A",axis=1)

df.loc[dates[2],"C"]=0

df.iloc[0:1]=0

df.loc[:,"B"]=np.arange(0,len(df))

new_index=pd.date_range("20160102",periods=7)

df.reindex(new_index,columns=list("ABCD"))

s1=pd.Series([1,2,3],index=list("ABC"))

s2=pd.Series([4,5,6],index=list("BCD"))

df1=pd.DataFrame(np.arange(1,13).reshape(3,4),index=list("abc"),columns=list("ABCD"))

df2=pd.DataFrame(np.arange(1,13).reshape(4,3),index=list("bcde"),columns=list("CDE"))

df1.apply(max,axis=1)

f=lambda x: x.max()-x.min()

df=df.apply(f,axis=1)

df3=df1.mul(df2,fill_value=0)

print(df3)

df4=df3.B[df3.B.notnull()]

df3.fillna(method="bfill")

df3.dropna(axis=1)

df3.duplicated()

df3=df3.drop_duplicates()

print(df3)



iloc[a:b,c:d] 从a到b-1行c到d-1列

axis=0 行 axis=1列

你可能感兴趣的:(2018-10-22)