访问特定日期并获取前一天的收盘价

huangapple go评论67阅读模式
英文:

Accessing a specific date and got the previous close

问题

这是我的数据框:

         timestamp    open    high     low  close      volume
    0   2023-01-03  3.5000  3.5972  3.3700  3.500     25231.0
    1   2023-01-04  3.5000  3.5500  3.3000  3.505     40815.0
    2   2023-01-05  3.5800  3.5800  3.4200  3.450     23833.0
    3   2023-01-06  3.4500  3.5400  3.4300  3.500     12298.0
    4   2023-01-09  3.5200  4.1500  3.4500  4.150     84826.0
    5   2023-01-10  4.1800  4.1800  3.9000  4.010     26032.0
    6   2023-01-11  4.1000  4.1000  3.9000  4.000     34807.0
    7   2023-01-12  3.9900  4.4200  3.9500  4.190     39737.0
    8   2023-01-13  4.4700  4.7500  4.3250  4.540     70672.0
    9   2023-01-17  4.8100  5.4000  4.7100  4.780    213792.0
    10  2023-01-18  4.6700  4.7475  4.3300  4.380     60063.0
    11  2023-01-19  4.3400  4.6100  4.3400  4.420     29150.0
    12  2023-01-20  4.3600  4.4400  4.3000  4.300     22088.0
    13  2023-01-23  4.2500  4.3242  4.1118  4.150     25304.0
    14  2023-01-24  4.2115  4.2499  4.1400  4.170     11240.0
    15  2023-01-25  4.3050  4.5000  4.1700  4.220     35726.0
    16  2023-01-26  4.2500  4.3000  3.8400  3.980     29257.0
    17  2023-01-27  3.8800  4.0200  3.7400  3.770     47719.0
    18  2023-01-30  3.7700  3.8700  3.4000  3.480     42704.0
    19  2023-01-31  3.4000  3.6599  3.3100  3.570    275723.0
    20  2023-02-01  3.4900  3.6500  3.4900  3.600     10763.0
    21  2023-02-02  3.7500  3.7800  3.6300  3.750     51914.0
    22  2023-02-03  3.7900  3.8899  3.6500  3.790     21763.0
    23  2023-02-06  3.9000  4.0600  3.6500  3.700     37767.0
    24  2023-02-07  3.7400  3.8500  3.5000  3.660     18919.0
    25  2023-02-08  3.6900  3.6900  3.5500  3.610      7878.0
    26  2023-02-09  3.6000  3.6000  3.5000  3.560      8594.0
    27  2023-02-10  3.5000  3.5500  3.3102  3.420     34729.0
    28  2023-02-13  3.4800  3.4800  3.1750  3.300    100681.0
    29  2023-02-14  3.2800  3.3400  3.1300  3.180     76240.0
    30  2023-02-15  3.2400  3.3000  3.1700  3.290     32600.0
    31  2023-02-16  3.4400  3.8800  3.3000  3.880    133655.0
    32  2023-02-17  3.9200  4.4300  3.9200  4.350    149117.0
    33  2023-02-21  6.5300  7.6100  6.0600  6.400  40559286.0

你可以使用以下代码来获取日期为2023-02-21的行,并获得前一个收盘价:

new_df = df[df['timestamp'] == '2023-02-21']
previous_close = df[df['timestamp'] == '2023-02-21']['close'].iloc[0]

这将在new_df中包含日期为2023-02-21的行,然后通过previous_close变量获取前一个收盘价。

英文:

Here is my dataframe:

     timestamp    open    high     low  close      volume
0   2023-01-03  3.5000  3.5972  3.3700  3.500     25231.0
1   2023-01-04  3.5000  3.5500  3.3000  3.505     40815.0
2   2023-01-05  3.5800  3.5800  3.4200  3.450     23833.0
3   2023-01-06  3.4500  3.5400  3.4300  3.500     12298.0
4   2023-01-09  3.5200  4.1500  3.4500  4.150     84826.0
5   2023-01-10  4.1800  4.1800  3.9000  4.010     26032.0
6   2023-01-11  4.1000  4.1000  3.9000  4.000     34807.0
7   2023-01-12  3.9900  4.4200  3.9500  4.190     39737.0
8   2023-01-13  4.4700  4.7500  4.3250  4.540     70672.0
9   2023-01-17  4.8100  5.4000  4.7100  4.780    213792.0
10  2023-01-18  4.6700  4.7475  4.3300  4.380     60063.0
11  2023-01-19  4.3400  4.6100  4.3400  4.420     29150.0
12  2023-01-20  4.3600  4.4400  4.3000  4.300     22088.0
13  2023-01-23  4.2500  4.3242  4.1118  4.150     25304.0
14  2023-01-24  4.2115  4.2499  4.1400  4.170     11240.0
15  2023-01-25  4.3050  4.5000  4.1700  4.220     35726.0
16  2023-01-26  4.2500  4.3000  3.8400  3.980     29257.0
17  2023-01-27  3.8800  4.0200  3.7400  3.770     47719.0
18  2023-01-30  3.7700  3.8700  3.4000  3.480     42704.0
19  2023-01-31  3.4000  3.6599  3.3100  3.570    275723.0
20  2023-02-01  3.4900  3.6500  3.4900  3.600     10763.0
21  2023-02-02  3.7500  3.7800  3.6300  3.750     51914.0
22  2023-02-03  3.7900  3.8899  3.6500  3.790     21763.0
23  2023-02-06  3.9000  4.0600  3.6500  3.700     37767.0
24  2023-02-07  3.7400  3.8500  3.5000  3.660     18919.0
25  2023-02-08  3.6900  3.6900  3.5500  3.610      7878.0
26  2023-02-09  3.6000  3.6000  3.5000  3.560      8594.0
27  2023-02-10  3.5000  3.5500  3.3102  3.420     34729.0
28  2023-02-13  3.4800  3.4800  3.1750  3.300    100681.0
29  2023-02-14  3.2800  3.3400  3.1300  3.180     76240.0
30  2023-02-15  3.2400  3.3000  3.1700  3.290     32600.0
31  2023-02-16  3.4400  3.8800  3.3000  3.880    133655.0
32  2023-02-17  3.9200  4.4300  3.9200  4.350    149117.0
33  2023-02-21  6.5300  7.6100  6.0600  6.400  40559286.0

I can access the row associate with the date 2023-02-21 in using new_df = df.loc[olhc_df["timestamp"] == datetime.date(2023,2,21).strftime("%Y-%m-%d")]

I would like to access the previous close. How can I do it? My idea is to point on the row associate with the date 2023-02-21 and just get the previous close.

I tried using new_df.shift(-1).close, but I obviously got something wrong.

答案1

得分: 0

df['timestamp'] = pd.to_datetime(df['timestamp'])
df['prev_close'] = df.close.shift(1)

timestamp      open    high     low    close     volume  prev_close
0  2023-01-03  3.5000  3.5972  3.3700  3.500     25231         NaN
1  2023-01-04  3.5000  3.5500  3.3000  3.505     40815       3.500
2  2023-01-05  3.5800  3.5800  3.4200  3.450     23833       3.505
3  2023-01-06  3.4500  3.5400  3.4300  3.500     12298       3.450
4  2023-01-09  3.5200  4.1500  3.4500  4.150     84826       3.500
5  2023-01-10  4.1800  4.1800  3.9000  4.010     26032       4.150
6  2023-01-11  4.1000  4.1000  3.9000  4.000     34807       4.010
7  2023-01-12  3.9900  4.4200  3.9500  4.190     39737       4.000
8  2023-01-13  4.4700  4.7500  4.3250  4.540     70672       4.190
9  2023-01-17  4.8100  5.4000  4.7100  4.780    213792       4.540
10 2023-01-18  4.6700  4.7475  4.3300  4.380     60063       4.780
英文:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['prev_close'] = df.close.shift(1)


timestamp      open    high     low    close     volume  prev_close
0  2023-01-03  3.5000  3.5972  3.3700  3.500     25231         NaN
1  2023-01-04  3.5000  3.5500  3.3000  3.505     40815       3.500
2  2023-01-05  3.5800  3.5800  3.4200  3.450     23833       3.505
3  2023-01-06  3.4500  3.5400  3.4300  3.500     12298       3.450
4  2023-01-09  3.5200  4.1500  3.4500  4.150     84826       3.500
5  2023-01-10  4.1800  4.1800  3.9000  4.010     26032       4.150
6  2023-01-11  4.1000  4.1000  3.9000  4.000     34807       4.010
7  2023-01-12  3.9900  4.4200  3.9500  4.190     39737       4.000
8  2023-01-13  4.4700  4.7500  4.3250  4.540     70672       4.190
9  2023-01-17  4.8100  5.4000  4.7100  4.780    213792       4.540
10 2023-01-18  4.6700  4.7475  4.3300  4.380     60063       4.780

huangapple
  • 本文由 发表于 2023年2月23日 22:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定