简体   繁体   中英

Price by day to Price by Week on Python Dict

I have a dictionary of some stock price for the last 252 business days. This dictionary contains 252 items with the structure timestamp=price .

year_prices = {'1584662400000': 85.5059967041, '1584921600000': 86.858001709, '1585008000000': 101.0, '1585094400000': 107.8499984741, '1585180800000': 105.6320037842, '1585267200000': 102.8720016479, '1585526400000': 100.4260025024, '1585612800000': 104.8000030518, '1585699200000': 96.31199646, '1585785600000': 90.8939971924, '1585872000000': 96.0019989014, '1586131200000': 103.2480010986, '1586217600000': 109.0899963379, '1586304000000': 109.7679977417, '1586390400000': 114.5999984741, '1586736000000': 130.1900024414, '1586822400000': 141.9779968262, '1586908800000': 145.966003418, '1586995200000': 149.0420074463, '1587081600000': 150.7779998779, '1587340800000': 149.2720031738, '1587427200000': 137.3439941406, '1587513600000': 146.4219970703, '1587600000000': 141.1260070801, '1587686400000': 145.0299987793, '1587945600000': 159.75, '1588032000000': 153.824005127, '1588118400000': 160.1020050049, '1588204800000': 156.3760070801, '1588291200000': 140.2640075684, '1588550400000': 152.2380065918, '1588636800000': 153.641998291, '1588723200000': 156.5160064697, '1588809600000': 156.0079956055, '1588896000000': 163.8840026855, '1589155200000': 162.2579956055, '1589241600000': 161.8820037842, '1589328000000': 158.1920013428, '1589414400000': 160.6660003662, '1589500800000': 159.8339996338, '1589760000000': 162.7259979248, '1589846400000': 161.6020050049, '1589932800000': 163.1119995117, '1590019200000': 165.5200042725, '1590105600000': 163.3760070801, '1590451200000': 163.7740020752, '1590537600000': 164.046005249, '1590624000000': 161.1620025635, '1590710400000': 167.0, '1590969600000': 179.6199951172, '1591056000000': 176.31199646, '1591142400000': 176.5919952393, '1591228800000': 172.8760070801, '1591315200000': 177.1320037842, '1591574400000': 189.9839935303, '1591660800000': 188.1340026855, '1591747200000': 205.0099945068, '1591833600000': 194.5679931641, '1591920000000': 187.0559997559, '1592179200000': 198.1799926758, '1592265600000': 196.425994873, '1592352000000': 198.358001709, '1592438400000': 200.7920074463, '1592524800000': 200.1799926758, '1592784000000': 198.8639984131, '1592870400000': 200.3560028076, '1592956800000': 192.1699981689, '1593043200000': 197.1959991455, '1593129600000': 191.9479980469, '1593388800000': 201.8699951172, '1593475200000': 215.9620056152, '1593561600000': 223.925994873, '1593648000000': 241.7319946289, '1593993600000': 274.3160095215, '1594080000000': 277.9719848633, '1594166400000': 273.175994873, '1594252800000': 278.8559875488, '1594339200000': 308.9299926758, '1594598400000': 299.4119873047, '1594684800000': 303.3599853516, '1594771200000': 309.2019958496, '1594857600000': 300.1279907227, '1594944000000': 300.1679992676, '1595203200000': 328.6000061035, '1595289600000': 313.6719970703, '1595376000000': 318.466003418, '1595462400000': 302.6140136719, '1595548800000': 283.3999938965, '1595808000000': 307.9200134277, '1595894400000': 295.2980041504, '1595980800000': 299.8219909668, '1596067200000': 297.4979858398, '1596153600000': 286.1520080566, '1596412800000': 297.0, '1596499200000': 297.3999938965, '1596585600000': 297.0039978027, '1596672000000': 297.9159851074, '1596758400000': 290.5419921875, '1597017600000': 283.7139892578, '1597104000000': 274.8779907227, '1597190400000': 310.9519958496, '1597276800000': 324.200012207, '1597363200000': 330.141998291, '1597622400000': 367.1279907227, '1597708800000': 377.4179992676, '1597795200000': 375.7059936523, '1597881600000': 400.3659973145, '1597968000000': 409.9960021973, '1598227200000': 402.8399963379, '1598313600000': 404.6679992676, '1598400000000': 430.6340026855, '1598486400000': 447.75, '1598572800000': 442.6799926758, '1598832000000': 498.3200073242, '1598918400000': 475.049987793, '1599004800000': 447.3699951172, '1599091200000': 407.0, '1599177600000': 418.3200073242, '1599523200000': 330.2099914551, '1599609600000': 366.2799987793, '1599696000000': 371.3399963379, '1599782400000': 372.7200012207, '1600041600000': 419.6199951172, '1600128000000': 449.7600097656, '1600214400000': 441.7600097656, '1600300800000': 423.4299926758, '1600387200000': 442.1499938965, '1600646400000': 449.3900146484, '1600732800000': 424.2300109863, '1600819200000': 380.3599853516, '1600905600000': 387.7900085449, '1600992000000': 407.3399963379, '1601251200000': 421.200012207, '1601337600000': 419.0700073242, '1601424000000': 429.0100097656, '1601510400000': 448.1600036621, '1601596800000': 415.0899963379, '1601856000000': 425.6799926758, '1601942400000': 413.9800109863, '1602028800000': 425.299987793, '1602115200000': 425.9200134277, '1602201600000': 434.0, '1602460800000': 442.299987793, '1602547200000': 446.6499938965, '1602633600000': 461.299987793, '1602720000000': 448.8800048828, '1602806400000': 439.6700134277, '1603065600000': 430.8299865723, '1603152000000': 421.9400024414, '1603238400000': 422.6400146484, '1603324800000': 425.7900085449, '1603411200000': 420.6300048828, '1603670400000': 420.2799987793, '1603756800000': 424.6799926758, '1603843200000': 406.0199890137, '1603929600000': 410.8299865723, '1604016000000': 388.0400085449, '1604275200000': 400.5100097656, '1604361600000': 423.8999938965, '1604448000000': 420.9800109863, '1604534400000': 438.0899963379, '1604620800000': 429.950012207, '1604880000000': 421.2600097656, '1604966400000': 410.3599853516, '1605052800000': 417.1300048828, '1605139200000': 411.7600097656, '1605225600000': 408.5, '1605484800000': 408.0899963379, '1605571200000': 441.6099853516, '1605657600000': 486.6400146484, '1605744000000': 499.2699890137, '1605830400000': 489.6099853516, '1606089600000': 521.8499755859, '1606176000000': 555.3800048828, '1606262400000': 574.0, '1606435200000': 585.7600097656, '1606694400000': 567.5999755859, '1606780800000': 584.7600097656, '1606867200000': 568.8200073242, '1606953600000': 593.3800048828, '1607040000000': 599.0399780273, '1607299200000': 641.7600097656, '1607385600000': 649.8800048828, '1607472000000': 604.4799804688, '1607558400000': 627.0700073242, '1607644800000': 609.9899902344, '1607904000000': 639.8300170898, '1607990400000': 633.25, '1608076800000': 622.7700195312, '1608163200000': 655.9000244141, '1608249600000': 695.0, '1608508800000': 649.8599853516, '1608595200000': 640.3400268555, '1608681600000': 645.9799804688, '1608768000000': 661.7700195312, '1609113600000': 663.6900024414, '1609200000000': 665.9899902344, '1609286400000': 694.7800292969, '1609372800000': 705.6699829102, '1609718400000': 729.7700195312, '1609804800000': 735.1099853516, '1609891200000': 755.9799804688, '1609977600000': 816.0399780273, '1610064000000': 880.0200195312, '1610323200000': 811.1900024414, '1610409600000': 849.4400024414, '1610496000000': 854.4099731445, '1610582400000': 845.0, '1610668800000': 826.1599731445, '1611014400000': 844.549987793, '1611100800000': 850.450012207, '1611187200000': 844.9899902344, '1611273600000': 846.6400146484, '1611532800000': 880.799987793, '1611619200000': 883.0900268555, '1611705600000': 864.1599731445, '1611792000000': 835.4299926758, '1611878400000': 793.5300292969, '1612137600000': 839.8099975586, '1612224000000': 872.7899780273, '1612310400000': 854.6900024414, '1612396800000': 849.9899902344, '1612483200000': 852.2299804688, '1612742400000': 863.4199829102, '1612828800000': 849.4600219727, '1612915200000': 804.8200073242, '1613001600000': 811.6599731445, '1613088000000': 816.1199951172, '1613433600000': 796.2199707031, '1613520000000': 798.1500244141, '1613606400000': 787.3800048828, '1613692800000': 781.299987793, '1613952000000': 714.5, '1614038400000': 698.8400268555, '1614124800000': 742.0200195312, '1614211200000': 682.2199707031, '1614297600000': 675.5, '1614556800000': 718.4299926758, '1614643200000': 686.4400024414, '1614729600000': 653.200012207, '1614816000000': 621.4400024414, '1614902400000': 597.950012207, '1615161600000': 563.0, '1615248000000': 673.5800170898, '1615334400000': 668.0599975586, '1615420800000': 699.5999755859, '1615507200000': 693.7299804688, '1615766400000': 707.9400024414, '1615852800000': 676.8800048828, '1615939200000': 701.8099975586, '1616025600000': 653.1599731445, '1616112000000': 654.8699951172}

Now I need to process this dict and group their values by week, so that I get a 52 items long dictionary (of structure timestamp:price ) being price the average price for that week from the original dict.

I can't figure out a way of getting the last 52 weeks dates in python, and also I have dubts on how to go from having the dates to getting the prices by week on a pythonic way.

There are (at least) two possible solutions that will work.


Solution 1

By using pandas you can use the following steps:

  1. Convert the data to a DataFrame
  2. Cast the timestamp columns to a datetime object
  3. Filter the dates ( filter/select rows of pandas dataframe by timestamp column )

Code

import pandas as pd

df = pd.DataFrame(year_prices.items(), columns=['timestamp', 'price'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')

# Example filtering times
df = df[(df['timestamp'] > '2020-07-01') & (df['timestamp'] < '2021-03-21')]

Solution 2

You can iterate over the data yourself, and use the built in datetime libary.

See:

Code

import datetime
from pprint import pprint

start_date = datetime.datetime(2020, 7, 2)
end_date = datetime.datetime(2021, 3, 21)

records = {}
for milliseconds, price in year_prices.items():
    date = datetime.datetime.fromtimestamp(int(milliseconds) / 1000.)
    if start_date < date < end_date:
        records[date] = price

pprint(records)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM