2

I want to convert the index column of a dataframe into an array, but I'm not sure how to go about it. I have already converted the second column to array, but for some reason it doesn't work on the first column as well. Here is how I'm trying to convert the index column:

time = df1.as_matrix(columns = df1.columns[:,0])

But I get

too many indices for array

Here is my dataframe

df1
Out[13]: 
                                 0
2015-11-19 23:59:54.500 -20.186533
2015-11-19 23:59:54.625 -20.272575
2015-11-19 23:59:54.750 -20.185249
2015-11-19 23:59:54.875 -20.247126
2015-11-19 23:59:55.000 -20.205975
2015-11-19 23:59:55.125 -20.281376
2015-11-19 23:59:55.250 -20.238962
2015-11-19 23:59:55.375 -20.300100
2015-11-19 23:59:55.500 -20.311625
2015-11-19 23:59:55.625 -20.264126
2015-11-19 23:59:55.750 -20.266762
2015-11-19 23:59:55.875 -20.224825
2015-11-19 23:59:56.000 -20.211288
2015-11-19 23:59:56.125 -20.163288
2015-11-19 23:59:56.250 -20.254587
2015-11-19 23:59:56.375 -20.125738
2015-11-19 23:59:56.500 -20.146749
2015-11-19 23:59:56.625 -20.161976
2015-11-19 23:59:56.750 -20.126276
2015-11-19 23:59:56.875 -20.082863
2015-11-19 23:59:57.000 -20.030237
2015-11-19 23:59:57.125 -20.098312
2015-11-19 23:59:57.250 -20.146214
2015-11-19 23:59:57.375 -20.030476
2015-11-19 23:59:57.500 -20.018661
2015-11-19 23:59:57.625 -20.029900
2015-11-19 23:59:57.750 -19.970963
2015-11-19 23:59:57.875 -19.994637
2015-11-19 23:59:58.000 -20.097612
2015-11-19 23:59:58.125 -19.952700
4
  • For the given example dataframe, can you try time = df.as_matrix(columns = df.columns[:])? Commented Sep 19, 2017 at 1:42
  • That gives me the second column only, unfortunately. Commented Sep 19, 2017 at 1:43
  • Just a note on as_matrix for future references: "This method is provided for backwards compatibility. Generally, it is recommended to use .values." Commented Sep 19, 2017 at 1:44
  • @alexanderson - as of pandas' 0.24.0 - you can access the backing array of a pandas Index with .array and .to_numpy - please find an updated answer bellow. pandas 0.24.x release notes Commented Jan 25, 2019 at 19:22

3 Answers 3

8

You can just do df.index.values:

df = pd.DataFrame(index=['a', 'b', 'c'])

df.index.values
# array(['a', 'b', 'c'], dtype=object)
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried using this method, and I got a numpy.datetime64 which is good, becasue I know how to convert it to datetime from there. Thanks a lot for the help.
Cool. Glad it helps.
0

Try time = df1.as_matrix(columns=df1.columns[0:1]). It looks like columns should be a 1-dimensional array (well, actually, an Index), and giving two indices to a 1-dimensional array would give that error.

3 Comments

That gives the second row only, unfortunately.
It looks like Psidom's answer should work.
It did. Thanks for you help though.
0

according to pandas 0.24.x release notes: "Series.array and Index.array have been added for extracting the array backing a Series or Index... We haven’t removed or deprecated Series.values or DataFrame.values, but we highly recommend and using .array or .to_numpy()"

Looks like this major version update does exactly what you need :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.