Guest User

Untitled

a guest
May 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import pandas as pd
  2.  
  3.  
  4. def parse_dataset(filepath):
  5. """
  6.  
  7. :return:
  8. :param filepath: to original TXT file
  9. :return: pandas dataframe parsed and formated
  10. """
  11.  
  12. global df
  13. df = pd.DataFrame() # Empty dataframe to fill; with file data.
  14. path = filepath
  15. wid_ref = [
  16. 2, 8, 2, 12, 3, 12, 10, 3, 4, 13, 13, 13, 13, 13, 13, 13, 5, 18,
  17. 18, 13, 1, 8, 7, 13, 12, 3]
  18.  
  19. df = pd.read_fwf(path, widths=wid_ref, dtype=str, skiprows=1, skipfooter=1)
  20.  
  21. df.columns = [
  22. 'RecordType', 'Date', 'BDICode', 'Ticker', 'MarketType', 'FirmName',
  23. 'Especs', 'DueDate(futures)', 'Currency', 'OpenPrice', 'HighPrice',
  24. 'LowPrice', 'AvgPrice', 'ClosePrice', 'BestBid', 'BestAsk', '#Trades',
  25. '#BondTrades', 'Volume', 'ExecutionPrice(Futures)', 'CorrectionIndex',
  26. 'ExpiringDate', 'TickerRatio', 'ExecutionPricePoints',
  27. 'IsinCode', 'DistributionCode']
  28. df.Date = pd.to_datetime(df.Date)
  29. return df
  30.  
  31.  
  32. def translate_bdi(df):
  33. bdi = {
  34. '02': 'LOTE PADRAO',
  35. '05': 'SANCIONADAS PELOS REGULAMENTOS BMFBOVESPA',
  36. '06': 'CONCORDATARIAS',
  37. '07': 'RECUPERACAO EXTRAJUDICIAL',
  38. '08': 'RECUPERACAO JUDICIAL',
  39. '09': 'RAET - REGIME DE ADMINISTRACAO ESPECIAL TEMPORARIA',
  40. '10': 'DIREITOS E RECIBOS',
  41. '11': 'INTERVENCAO',
  42. '12': 'FUNDOS IMOBILIARIOS',
  43. '14': 'CERT.INVEST/TIT.DIV.PUBLICA',
  44. '18': 'OBRIGACOES',
  45. '22': 'BONUS (PRIVADOS)',
  46. '26': 'APOLICES/BONUS/TITULOS PUBLICOS',
  47. '32': 'EXERCICIO DE OPCOES DE COMPRA DE INDICES',
  48. '33': 'EXERCICIO DE OPCOES DE VENDA DE INDICES',
  49. '38': 'EXERCICIO DE OPCOES DE COMPRA',
  50. '42': 'EXERCICIO DE OPCOES DE VENDA',
  51. '46': 'LEILAO DE NAO COTADOS',
  52. '48': 'LEILAO DE PRIVATIZACAO',
  53. '49': 'LEILAO DO FUNDO RECUPERACAO ECONOMICA ESPIRITO SANTO',
  54. '50': 'LEILAO',
  55. '51': 'LEILAO FINOR',
  56. '52': 'LEILAO FINAM',
  57. '53': 'LEILAO FISET',
  58. '54': 'LEILAO DE ACÕES EM MORA',
  59. '56': 'VENDAS POR ALVARA JUDICIAL',
  60. '58': 'OUTROS',
  61. '60': 'PERMUTA POR ACÕES',
  62. '61': 'META',
  63. '62': 'MERCADO A TERMO',
  64. '66': 'DEBENTURES COM DATA DE VENCIMENTO ATE 3 ANOS',
  65. '68': 'DEBENTURES COM DATA DE VENCIMENTO MAIOR QUE 3 ANOS',
  66. '70': 'FUTURO COM RETENCAO DE GANHOS',
  67. '71': 'MERCADO DE FUTURO',
  68. '74': 'OPCOES DE COMPRA DE INDICES',
  69. '75': 'OPCOES DE VENDA DE INDICES',
  70. '78': 'OPCOES DE COMPRA',
  71. '82': 'OPCOES DE VENDA',
  72. '83': 'BOVESPAFIX',
  73. '84': 'SOMA FIX',
  74. '90': 'TERMO VISTA REGISTRADO',
  75. '96': 'MERCADO FRACIONARIO',
  76. '99': 'TOTAL GERAL'
  77. }
  78.  
  79. df['BDICode'].replace(bdi, inplace=True)
  80. return df
  81.  
  82.  
  83. def market_type(df):
  84. mkt = {
  85. '010': 'VISTA',
  86. '012': 'CALL EXERCISE',
  87. '013': 'PUT EXERCISE',
  88. '017': 'LEILAO',
  89. '020': 'FRACIONARIO',
  90. '030': 'TERMO',
  91. '050': 'FUTURO COM RETENCAO',
  92. '060': 'FUTURO CONTINUO',
  93. '070': 'CALL OPTION',
  94. '080': 'PUT OPTION'
  95. }
  96.  
  97. df['MarketType'].replace(mkt, inplace=True)
  98. return df
  99.  
  100.  
  101. def price_mod(df):
  102. df.OpenPrice = pd.to_numeric(df.OpenPrice) / 100
  103. df.HighPrice = pd.to_numeric(df.HighPrice) / 100
  104. df.LowPrice = pd.to_numeric(df.LowPrice) / 100
  105. df.AvgPrice = pd.to_numeric(df.AvgPrice) / 100
  106. df.ClosePrice = pd.to_numeric(df.ClosePrice) / 100
  107. df.BestBid = pd.to_numeric(df.BestBid) / 100
  108. df.BestAsk = pd.to_numeric(df.BestAsk) / 100
Add Comment
Please, Sign In to add comment