How to copy dataframe while keeping C-Contiguos arrangement of data?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















pandas dataframe copy method returns a dataframe with data organized as F-Contiguos, even if the original dataframe was arranged as C-Contiguos.



Example:



In [1]: import numpy
...: import pandas
...: easy_matrix_example = numpy.array([
...: [100, 20200, 20900 ],
...: [200, 80200, 80900 ],
...: [300, 180200, 180900 ],
...: [400, 320200, 320900 ],
...: [500, 500200, 500900 ],
...: ], dtype=numpy.float64)
...: easy_df_example = pandas.DataFrame(easy_matrix_example, columns=["A","B","C"])
...:
...:

In [2]: easy_df_example
Out[2]:
A B C
0 100.0 20200.0 20900.0
1 200.0 80200.0 80900.0
2 300.0 180200.0 180900.0
3 400.0 320200.0 320900.0
4 500.0 500200.0 500900.0

In [5]: easy_df_example.values.flags
Out[5]:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

In [6]: df_copy = easy_df_example.copy()

In [7]: df_copy
Out[7]:
A B C
0 100.0 20200.0 20900.0
1 200.0 80200.0 80900.0
2 300.0 180200.0 180900.0
3 400.0 320200.0 320900.0
4 500.0 500200.0 500900.0

In [8]: df_copy.values.flags
Out[8]:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False


What's the recommended approach to copying a C-Contiguos dataframe and obtaining another C-Contiguous dataframe?










share|improve this question





























    1















    pandas dataframe copy method returns a dataframe with data organized as F-Contiguos, even if the original dataframe was arranged as C-Contiguos.



    Example:



    In [1]: import numpy
    ...: import pandas
    ...: easy_matrix_example = numpy.array([
    ...: [100, 20200, 20900 ],
    ...: [200, 80200, 80900 ],
    ...: [300, 180200, 180900 ],
    ...: [400, 320200, 320900 ],
    ...: [500, 500200, 500900 ],
    ...: ], dtype=numpy.float64)
    ...: easy_df_example = pandas.DataFrame(easy_matrix_example, columns=["A","B","C"])
    ...:
    ...:

    In [2]: easy_df_example
    Out[2]:
    A B C
    0 100.0 20200.0 20900.0
    1 200.0 80200.0 80900.0
    2 300.0 180200.0 180900.0
    3 400.0 320200.0 320900.0
    4 500.0 500200.0 500900.0

    In [5]: easy_df_example.values.flags
    Out[5]:
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : False
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False
    UPDATEIFCOPY : False

    In [6]: df_copy = easy_df_example.copy()

    In [7]: df_copy
    Out[7]:
    A B C
    0 100.0 20200.0 20900.0
    1 200.0 80200.0 80900.0
    2 300.0 180200.0 180900.0
    3 400.0 320200.0 320900.0
    4 500.0 500200.0 500900.0

    In [8]: df_copy.values.flags
    Out[8]:
    C_CONTIGUOUS : False
    F_CONTIGUOUS : True
    OWNDATA : False
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False
    UPDATEIFCOPY : False


    What's the recommended approach to copying a C-Contiguos dataframe and obtaining another C-Contiguous dataframe?










    share|improve this question

























      1












      1








      1








      pandas dataframe copy method returns a dataframe with data organized as F-Contiguos, even if the original dataframe was arranged as C-Contiguos.



      Example:



      In [1]: import numpy
      ...: import pandas
      ...: easy_matrix_example = numpy.array([
      ...: [100, 20200, 20900 ],
      ...: [200, 80200, 80900 ],
      ...: [300, 180200, 180900 ],
      ...: [400, 320200, 320900 ],
      ...: [500, 500200, 500900 ],
      ...: ], dtype=numpy.float64)
      ...: easy_df_example = pandas.DataFrame(easy_matrix_example, columns=["A","B","C"])
      ...:
      ...:

      In [2]: easy_df_example
      Out[2]:
      A B C
      0 100.0 20200.0 20900.0
      1 200.0 80200.0 80900.0
      2 300.0 180200.0 180900.0
      3 400.0 320200.0 320900.0
      4 500.0 500200.0 500900.0

      In [5]: easy_df_example.values.flags
      Out[5]:
      C_CONTIGUOUS : True
      F_CONTIGUOUS : False
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False
      UPDATEIFCOPY : False

      In [6]: df_copy = easy_df_example.copy()

      In [7]: df_copy
      Out[7]:
      A B C
      0 100.0 20200.0 20900.0
      1 200.0 80200.0 80900.0
      2 300.0 180200.0 180900.0
      3 400.0 320200.0 320900.0
      4 500.0 500200.0 500900.0

      In [8]: df_copy.values.flags
      Out[8]:
      C_CONTIGUOUS : False
      F_CONTIGUOUS : True
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False
      UPDATEIFCOPY : False


      What's the recommended approach to copying a C-Contiguos dataframe and obtaining another C-Contiguous dataframe?










      share|improve this question














      pandas dataframe copy method returns a dataframe with data organized as F-Contiguos, even if the original dataframe was arranged as C-Contiguos.



      Example:



      In [1]: import numpy
      ...: import pandas
      ...: easy_matrix_example = numpy.array([
      ...: [100, 20200, 20900 ],
      ...: [200, 80200, 80900 ],
      ...: [300, 180200, 180900 ],
      ...: [400, 320200, 320900 ],
      ...: [500, 500200, 500900 ],
      ...: ], dtype=numpy.float64)
      ...: easy_df_example = pandas.DataFrame(easy_matrix_example, columns=["A","B","C"])
      ...:
      ...:

      In [2]: easy_df_example
      Out[2]:
      A B C
      0 100.0 20200.0 20900.0
      1 200.0 80200.0 80900.0
      2 300.0 180200.0 180900.0
      3 400.0 320200.0 320900.0
      4 500.0 500200.0 500900.0

      In [5]: easy_df_example.values.flags
      Out[5]:
      C_CONTIGUOUS : True
      F_CONTIGUOUS : False
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False
      UPDATEIFCOPY : False

      In [6]: df_copy = easy_df_example.copy()

      In [7]: df_copy
      Out[7]:
      A B C
      0 100.0 20200.0 20900.0
      1 200.0 80200.0 80900.0
      2 300.0 180200.0 180900.0
      3 400.0 320200.0 320900.0
      4 500.0 500200.0 500900.0

      In [8]: df_copy.values.flags
      Out[8]:
      C_CONTIGUOUS : False
      F_CONTIGUOUS : True
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False
      UPDATEIFCOPY : False


      What's the recommended approach to copying a C-Contiguos dataframe and obtaining another C-Contiguous dataframe?







      python pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 23:25









      AlechanAlechan

      128112




      128112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          you can create a new object copying the underlying value yourself.



          example:



          df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
          df2 = pd.DataFrame(df.values.copy(), columns=df.columns)
          df2.values.flags
          outputs:
          C_CONTIGUOUS : True
          F_CONTIGUOUS : False
          OWNDATA : False
          WRITEABLE : True
          ALIGNED : True
          WRITEBACKIFCOPY : False
          UPDATEIFCOPY : False





          share|improve this answer
























          • what about the index, dtype, etc?

            – Alechan
            Nov 22 '18 at 23:46











          • the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

            – Haleemur Ali
            Nov 22 '18 at 23:52











          • what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

            – Alechan
            Nov 23 '18 at 0:06












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439046%2fhow-to-copy-dataframe-while-keeping-c-contiguos-arrangement-of-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          you can create a new object copying the underlying value yourself.



          example:



          df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
          df2 = pd.DataFrame(df.values.copy(), columns=df.columns)
          df2.values.flags
          outputs:
          C_CONTIGUOUS : True
          F_CONTIGUOUS : False
          OWNDATA : False
          WRITEABLE : True
          ALIGNED : True
          WRITEBACKIFCOPY : False
          UPDATEIFCOPY : False





          share|improve this answer
























          • what about the index, dtype, etc?

            – Alechan
            Nov 22 '18 at 23:46











          • the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

            – Haleemur Ali
            Nov 22 '18 at 23:52











          • what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

            – Alechan
            Nov 23 '18 at 0:06
















          0














          you can create a new object copying the underlying value yourself.



          example:



          df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
          df2 = pd.DataFrame(df.values.copy(), columns=df.columns)
          df2.values.flags
          outputs:
          C_CONTIGUOUS : True
          F_CONTIGUOUS : False
          OWNDATA : False
          WRITEABLE : True
          ALIGNED : True
          WRITEBACKIFCOPY : False
          UPDATEIFCOPY : False





          share|improve this answer
























          • what about the index, dtype, etc?

            – Alechan
            Nov 22 '18 at 23:46











          • the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

            – Haleemur Ali
            Nov 22 '18 at 23:52











          • what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

            – Alechan
            Nov 23 '18 at 0:06














          0












          0








          0







          you can create a new object copying the underlying value yourself.



          example:



          df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
          df2 = pd.DataFrame(df.values.copy(), columns=df.columns)
          df2.values.flags
          outputs:
          C_CONTIGUOUS : True
          F_CONTIGUOUS : False
          OWNDATA : False
          WRITEABLE : True
          ALIGNED : True
          WRITEBACKIFCOPY : False
          UPDATEIFCOPY : False





          share|improve this answer













          you can create a new object copying the underlying value yourself.



          example:



          df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
          df2 = pd.DataFrame(df.values.copy(), columns=df.columns)
          df2.values.flags
          outputs:
          C_CONTIGUOUS : True
          F_CONTIGUOUS : False
          OWNDATA : False
          WRITEABLE : True
          ALIGNED : True
          WRITEBACKIFCOPY : False
          UPDATEIFCOPY : False






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 23:45









          Haleemur AliHaleemur Ali

          12.8k21741




          12.8k21741













          • what about the index, dtype, etc?

            – Alechan
            Nov 22 '18 at 23:46











          • the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

            – Haleemur Ali
            Nov 22 '18 at 23:52











          • what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

            – Alechan
            Nov 23 '18 at 0:06



















          • what about the index, dtype, etc?

            – Alechan
            Nov 22 '18 at 23:46











          • the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

            – Haleemur Ali
            Nov 22 '18 at 23:52











          • what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

            – Alechan
            Nov 23 '18 at 0:06

















          what about the index, dtype, etc?

          – Alechan
          Nov 22 '18 at 23:46





          what about the index, dtype, etc?

          – Alechan
          Nov 22 '18 at 23:46













          the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

          – Haleemur Ali
          Nov 22 '18 at 23:52





          the dtypes should carry over automatically, if you want to copy the index over as well, pass in index=df.index to the dataframe constructor

          – Haleemur Ali
          Nov 22 '18 at 23:52













          what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

          – Alechan
          Nov 23 '18 at 0:06





          what I meant was that the copy method took care of copying everything to make both dataframes equivalent. If I make the copy "by hand" then I have to make sure to copy all the metadata correctly and that may result in errors and my code is coupled to the pandas DataFrame representation.

          – Alechan
          Nov 23 '18 at 0:06




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439046%2fhow-to-copy-dataframe-while-keeping-c-contiguos-arrangement-of-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?