Home > Net >  Format date column
Format date column

Time:08-17

For reference, visit https://pythonprogramming.net/machine-learning-pattern-recognition-algorithmic-forex-stock-trading/

I have a text file containing date, bid, ask data as follows:

GBPUSD1d.txt -> 20130501000000,1.55358,1.55371
                20130501000000,1.55357,1.55369
                20130501000000,1.55353,1.55367

I am trying the following code to assign variables to the data

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np

date,bid,ask = np.loadtxt('GBPUSD1d.txt', unpack=True,
                          delimiter=',',
                          converters={0:mdates.strpdate2num('%Y%m%d%H%M%S')})

and I want to get the zeroth data points in the date format shown above.

But I get the following error:

AttributeError: module 'matplotlib.dates' has no attribute 'strpdate2num'

Is there another Pythonic way to do this? The tutorial in the link uses Python 2.7. I use Python 3.

CodePudding user response:

I don't know the datetime format you want so,

if the format it's already ok:

from datetime import datetime

date, bid, ask = np.loadtxt(
    "src/data.txt",
    unpack=True,
    delimiter=",",
    converters={
        0: lambda x: x.decode()
    },
)

if you want to change the format:

from datetime import datetime

date, bid, ask = np.loadtxt(
    "src/data.txt",
    unpack=True,
    delimiter=",",
    converters={
        0: lambda x: datetime.strptime(x.decode(), "%Y%m%d%H%M%S").strftime("%Y%m%d")
    },
)

I'm not 100% sure but it seems like the output of the lambda function must be a float or something that can be interpreted as so (20130501 it's ok but not 2013-05-01)

  • Related