python - Pandas 误解 CSV 文件中的日期列

标签 python pandas csv datetime

输入.csv,

第一列没有标题。 header 值类似于 (\tdate\tg1_o\tg1_h\tg1_l\tg1_c\tg2)

        date        g1_o    g1_h     g1_l   g1_c    g2
1945    01-11-2017  123.3   126.4   122.5   124.35  1540064.0
1946    02-11-2017  124.35  128.2   123.3   127.0   3962419.0
1947    03-11-2017  127.8   128.05  124.8   125.25  1672188.0
1948    06-11-2017  125.05  126.45  122.95  125.45  1457863.0
1949    07-11-2017  126.0   126.55  119.4   121.25  2404566.0
1950    08-11-2017  121.0   123.1   117.35  118.0   2737696.0
1951    09-11-2017  118.3   121.45  117.7   119.95  1512002.0
1952    10-11-2017  122.3   122.6   118.6   119.75  2630131.0
1953    13-11-2017  119.7   120.1   114.8   116.0   2407190.0
1954    14-11-2017  114.9   116.5   113.9   115.75  1228325.0
1955    15-11-2017  116.0   116.0   112.4   113.45  933757.0
1956    16-11-2017  114.4   116.25  113.0   115.45  1323516.0
1957    17-11-2017  116.95  117.6   115.05  116.25  1253531.0
1958    20-11-2017  118.3   120.95  116.5   120.2   3044296.0
1959    21-11-2017  120.85  120.95  118.1   119.05  947658.0
1960    22-11-2017  119.0   121.1   117.5   120.35  1875986.0
1961    23-11-2017  121.4   121.7   118.9   119.9   2099127.0
1962    24-11-2017  120.0   122.0   119.4   121.3   1425134.0
1963    27-11-2017  121.3   121.45  119.2   120.55  2021124.0
1964    28-11-2017  120.45  121.7   119.3   120.05  1770775.0
1965    29-11-2017  119.5   120.25  117.9   118.3   1021924.0
1966    30-11-2017  118.3   118.3   115.7   116.2   1848217.0
1967    01-12-2017  117.35  117.65  114.35  114.9   1183132.0

下面是为 g1_c 生成 DataFrame 的代码,即 close 和给定的日期范围

def create_date_range():
    start = "2017-11-01"
    end = "2017-12-01"
    return pd.date_range(start, end)


def get_stocks_data(symbol):
    df1 = pd.read_csv("../../Stock_data/{}.csv".format(symbol),
                      sep='\t',
                      lineterminator='\n',
                      usecols=["date", "g1_c"],
                      index_col="date",
                      parse_dates=True
                      )
    df1 = df1.rename(columns={"g1_c": symbol})    
    return df

ind = create_date_range()
df = pd.DataFrame(index=ind)
symbols = ["input"]
d = get_stocks_data(symbols[0])
p = df.join(d, how="inner")
print(p)

IT 提供以下输出,但值 2017-11-01 81.00 但 input.csv 文件包含 2017-11-01< 的数据124.35,有很多地方数据不匹配。对于某些日期,如 2017-11-302017-11-292017-11-28 等,输出是正确的。

并且2017-11-022017-11-03也没有结果,我认为连接存在一些问题。

               input
2017-11-01   81.00
2017-11-04   98.10
2017-11-05  101.70
2017-11-07  100.50
2017-11-08  108.60
2017-11-09  124.95
2017-11-10  118.85
2017-11-12  114.65
2017-11-13  116.00
2017-11-14  115.75
2017-11-15  113.45
2017-11-16  115.45
2017-11-17  116.25
2017-11-20  120.20
2017-11-21  119.05
2017-11-22  120.35
2017-11-23  119.90
2017-11-24  121.30
2017-11-27  120.55
2017-11-28  120.05
2017-11-29  118.30
2017-11-30  116.20
2017-12-01   83.10

为什么数据不一致,仅针对某些日期?

最佳答案

看来您的日期是从第一天开始的。 pandas 可能会以这种方式误解您的日期,因此请确保在加载 csv 时指定 dayfirst=True -

df1 = pd.read_csv(
      "../../Stock_data/{}.csv".format(symbol),
      sep='\t',
      lineterminator='\n',
      usecols=["date", "g1_c"],
      index_col="date",
      parse_dates=True,
      <b>dayfirst=True</b>
)

dayfirst 参数被转发到日期解析器库(它解析 pandas 的日期),该库根据需要处理日期。

关于python - Pandas 误解 CSV 文件中的日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047935/

相关文章:

python - Pandas/matplotlib 并未绘制所有列数据

python : How do I easily get sub-totals on row and column?

java - 加载 csv 文件时过滤特定行

excel - 在 Matlab 中检索 Xlsx 或 csv

c# - 如何按值中的子字符串对 Dictionary<string,string> 进行排序?

python - Django templateTag 中的上下文 KeyError

Python Tensorflow NoneType 不可迭代

python - 检查字符串是否包含python中的日期或时间戳

python - 使用Python创建三维矩阵结构并写入mat文件

python - 将列转置为行,将前列的 value_counts 显示为 Pandas 中的列值