Skip to content

Commit

Permalink
https://github.com/pandas-dev/pandas/issues/53235
Browse files Browse the repository at this point in the history
  • Loading branch information
JPL-JUNO committed Apr 10, 2024
1 parent b9e3854 commit cc2053a
Showing 1 changed file with 254 additions and 0 deletions.
254 changes: 254 additions & 0 deletions EPY/t.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"\n",
"df_time = pd.DataFrame({'B': [0, 1, 2, 2, 4]},\n",
" index=[pd.Timestamp('20130101 09:00:00'),\n",
" pd.Timestamp('20130101 09:00:02'),\n",
" pd.Timestamp('20130101 09:00:03'),\n",
" pd.Timestamp('20130101 09:00:05'),\n",
" pd.Timestamp('20130101 09:00:06')])"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"df_time = df_time.reset_index()"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>index</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2013-01-01 09:00:00</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2013-01-01 09:00:02</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2013-01-01 09:00:03</td>\n",
" <td>2</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2013-01-01 09:00:05</td>\n",
" <td>2</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2013-01-01 09:00:06</td>\n",
" <td>4</td>\n",
" <td>16</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" index B C\n",
"0 2013-01-01 09:00:00 0 0\n",
"1 2013-01-01 09:00:02 1 1\n",
"2 2013-01-01 09:00:03 2 4\n",
"3 2013-01-01 09:00:05 2 4\n",
"4 2013-01-01 09:00:06 4 16"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_time['C'] = df_time['B'] ** 2\n",
"df_time"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"def rolling(window_df, df):\n",
" data = df.loc[window_df.index]\n",
" if data['C'].iloc[1] == data['C'].iloc[0]:\n",
" return 1000\n",
" return data['C'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 1.0\n",
"2 5.0\n",
"3 1000.0\n",
"4 20.0\n",
"Name: B, dtype: float64"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_time['B'].rolling(window=2).apply(rolling, args=(df_time,))"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"rolled = df_time.rolling(window=2)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"index = []\n",
"values = []\n",
"for r in rolled:\n",
" index.append(r.index[-1])\n",
" if len(r) < 2:\n",
" values.append(np.nan)\n",
" else:\n",
" if r['B'].is_unique:\n",
" # 各不相同\n",
" # 计算收益率\n",
" values.append(1000)\n",
" else:\n",
" values.append(0)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([0, 1, 2, 3, 4], [nan, 1000, 1000, 0, 1000])"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index, values"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3 2\n",
"4 4\n",
"Name: B, dtype: int64"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r['B']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit cc2053a

Please sign in to comment.