赋值何时在Python中执行深层复制?

时间:2016-12-03 11:06:06

标签: python pandas hdf5

我有以下代码:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

 <div class="panel panel-default set_margin_0 set_padding_0" >
                <div class="panel-heading text-left ticket_panel_body_bg">
                    <div class="row">
                        <div class="col-md-10 ">
                            <span>Add Discount </span>
                        </div>

                        <div class="col-md-2 ">
                            <span class="pull-right cursor_pointer open_discount_div">+</span>
                        </div>
                    </div>
                </div>



              <div class="panel-body set_padding_0">
                    DIscount Type , Code
                </div>

                <div class="panel-body set_padding_0 show_hide_panel">
                        Data To be displayed on clicking + symbol
                </div>

在这种情况下,import pandas as pd store = pd.HDFStore('cache.h5') data = store['data'] 是HDF5数据的深层内存副本,还是指向磁盘上原始数据的指针?

1 个答案:

答案 0 :(得分:1)

它是内存对象&#34;,它不会自动反映(刷新)到磁盘。

演示:

In [16]: fn = r'D:\temp\.data\test.h5'

In [17]: store = pd.HDFStore(fn)

In [18]: store
Out[18]:
<class 'pandas.io.pytables.HDFStore'>
File path: D:\temp\.data\test.h5
/df2             frame_table  (typ->appendable,nrows->7,ncols->4,indexers->[index],dc->[Col1,Col2,Col3,Col4])
/test            frame_table  (typ->appendable,nrows->7,ncols->4,indexers->[index],dc->[Col1,Col2,Col3,Col4])

从磁盘(HDF Store)读入DataFrame(内存中的对象):

In [19]: data = store['test']

In [20]: data
Out[20]:
        Col1      Col2  Col3  Col4
0       what       the     0     0
1        are    curves     1     8
2        men        of     2    16
3         to      your     3    24
4      rocks      lips     4    32
5        and   rewrite     5    40
6  mountains  history.     6    48

In [21]: data.Col4 = 1000

In [22]: data
Out[22]:
        Col1      Col2  Col3  Col4
0       what       the     0  1000
1        are    curves     1  1000
2        men        of     2  1000
3         to      your     3  1000
4      rocks      lips     4  1000
5        and   rewrite     5  1000
6  mountains  history.     6  1000

In [23]: store.close()

In [24]: store = pd.HDFStore(fn)

In [25]: store['test']
Out[25]:
        Col1      Col2  Col3  Col4
0       what       the     0     0
1        are    curves     1     8
2        men        of     2    16
3         to      your     3    24
4      rocks      lips     4    32
5        and   rewrite     5    40
6  mountains  history.     6    48

更新:以下小型演示显示data DF在从HDF商店中读取后不依赖于store

In [26]: store.close()

In [27]: store = pd.HDFStore(fn)

In [28]: del data

In [29]: data = store['test']

让我们删除store对象

In [30]: del store

data仍在那里

In [31]: data
Out[31]:
        Col1      Col2  Col3  Col4
0       what       the     0     0
1        are    curves     1     8
2        men        of     2    16
3         to      your     3    24
4      rocks      lips     4    32
5        and   rewrite     5    40
6  mountains  history.     6    48