You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas DateRange未覆盖CSV剩余数据及比特币数据处理求助

Hey there! Let's break down what's going wrong here and fix it step by step—since you're new to Pandas, we'll keep this clear and actionable.

The Core Issue With Your Current Code

Right now, you're replacing the original data's index entirely with a custom date range using pd.date_range(DATA_INICIO, DATA_FIM). This doesn't "extract" data from your CSV—it forces your dataset to fit that fixed date range, which is why you're losing any data outside that window (and potentially misaligning existing data if the timestamps don't match perfectly).

Your CSV already has timestamp data built in—we just need to leverage that column to filter the time range you want, instead of overwriting the index.

Fixed Implementation

Here's the corrected code, with explanations for each step:

from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.style.use('fivethirtyeight')
DT_FILE_PATH = 'bitstamp.csv'
DATA_INICIO = '2000-10-10'
DATA_FIM = '2010-10-10'

def getDataSetFile(file_path):
    # 1. Read the CSV and parse the timestamp column into datetime objects
    # Replace 'Timestamp' with the actual name of the time column in your CSV (e.g., 'Date')
    dt_bitstamp = pd.read_csv(
        file_path,
        parse_dates=['Timestamp'],
        index_col='Timestamp'  # Set the parsed time column as the DataFrame index
    )
    
    print("Original dataset preview:")
    print(dt_bitstamp.head())
    
    # 2. Filter data to your desired time range using the datetime index
    filtered_data = dt_bitstamp.loc[DATA_INICIO:DATA_FIM]
    
    print("\nFiltered dataset (your target time range):")
    print(filtered_data.head())
    
    return filtered_data

Key Explanations

  • parse_dates=['Timestamp']: Tells Pandas to convert the timestamp column from plain text/numbers into proper datetime objects—this is essential for time-based filtering. Double-check your CSV's time column name (it might be Date or UnixTimestamp instead of Timestamp) and adjust this parameter accordingly.
  • index_col='Timestamp': Makes the datetime column your DataFrame's index. This lets you use Pandas' intuitive loc syntax for time range filtering.
  • dt_bitstamp.loc[DATA_INICIO:DATA_FIM]: This is the standard way to slice time-series data in Pandas. It will extract all rows where the timestamp falls between DATA_INICIO and DATA_FIM, without losing any other data in your original CSV (you just return the filtered subset).

Handling Unix Timestamps (If Needed)

If your CSV uses Unix timestamps (integer values representing seconds/milliseconds since 1970), add this step right after reading the CSV to convert them to datetime:

# Convert Unix timestamp (in seconds) to datetime
dt_bitstamp['Timestamp'] = pd.to_datetime(dt_bitstamp['Timestamp'], unit='s')
# Use unit='ms' if your timestamps are in milliseconds

Next Steps for Calculating Technical Indicators

Once you have your filtered dataset, calculating indicators like moving averages, RSI, or MACD is straightforward. For example:

# Calculate a 20-day moving average (using the 'Close' price column)
filtered_data['MA20'] = filtered_data['Close'].rolling(window=20).mean()

# For more complex indicators like RSI, you can use libraries like `ta-lib` or implement custom functions

内容的提问来源于stack exchange,提问作者Lucas Silluzio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:41:41