TAYU Academy course material · Python Quant Trading Lab
Text walkthrough
Pandas basics
A self-paced text walkthrough, without video or audio. See the article below for worked examples and materials.
Course Notes
1. Understand the core idea of “Pandas basics” and explain it in your own words
2. Master this module's focus: Processing financial data with Pandas
3. Complete the assignment on your own and check it against the reference answer
Pandas' Series is a column with an index, while DataFrame organizes multiple columns; selection by label differs from selection by position. Time indices must be sorted and unique; row numbers cannot be used to represent trading time.
Core Steps: Read CSV, parse time to UTC, unify, remove duplicates, check for missing and anomalous values, calculate returns, then resample to a higher frequency. Time series calculations should avoid using future data; for example, use rolling with past data only.
Example: df['ret'] = df['close'].pct_change() to calculate returns; df.resample('1h').agg({'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'}) to resample minute data into hourly data.
Read CSV → View column names/types/number of rows → Parse UTC → Sort and de-duplicate → Set index → Select only necessary columns; retain original file and cleaning report.
Run `pandas_lab.py`, and write the number of rows, the reason for the first missing return, and the last SMA2 in three lines as `close=100/102/101`.
Three rows; the first row lacks the previous close and is missing; the final SMA2=(102+101)/2=101.5. The complete data should be 240 valid, 1 repeat, and 3 isolated, and missing values cannot be filled with 0.
Closing synthetic prices of 100, 102, and 101 correspond to missing gains of -0.00980392 and 0.02, respectively. If you copy the row of 101 and then calculate, you must first remove duplicates. SMA2 is in the second row of 101 and the third row of 101.5. Extra lagging is only required when the signal point is earlier than the calculation data; not all rolling calculations require a mechanical shift.
`df.shift(-1)` can be used for post-event labeling but cannot be used as a feature; do not fill in the first missing profit with a valid training sample.
Download the companion files into one folder, install the dependencies listed in the README, then run. The full script also prints a cleaning report and OHLC resampling; the snippet below uses real pandas APIs.
import pandas as pd
df = pd.read_csv('ohlcv-synthetic.csv')
df['timestamp'] = pd.to_datetime(df['timestamp'], utc=True)
df = df.sort_values('timestamp').drop_duplicates('timestamp')
df = df.set_index('timestamp')
df['return'] = df['close'].pct_change(fill_method=None)
df['sma_3'] = df['close'].rolling(3, min_periods=3).mean()
print(df[['close', 'return', 'sma_3']].head(8))1. Unify time zones and remove duplicates
2. Check data quality first
3. Avoid look-ahead bias
Risk warning: leveraged trading in forex, precious metals and similar products carries a high level of risk and you may lose all of your capital, or more. This course is for education only. It is not investment advice and does not promise any return. Example prices and contract specifications are teaching assumptions; always use what your own platform discloses. Past performance and backtests do not indicate future results.
All values, prices, accounts and cases in this course are teaching assumptions; the fixed materials can be reproduced offline. Marking a lesson complete only records your study. It is not a certification, a service approval or a promise of returns. Workbooks and answer files are currently provided in Chinese.
Key Takeaways
Completion records your own study, not verified competence. Exercises use fictional data; no real account or deposit is required.