question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inst.to_data_frame() should allow to export index as datetime64

See original GitHub issue

here is an example of script I just had to write for a collaborator:

from pathlib import Path
import pandas as pd
import mne

sample_dir = Path(mne.datasets.sample.data_path())
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

raw = mne.io.read_raw_fif(sample_fname, preload=True)
raw.crop(tmax=10)

df = raw.to_data_frame()
df = df.set_index("time")

index =  pd.date_range(start=raw.info['meas_date'],
                       periods=len(df) + raw.first_samp,
                       freq=f'{1e3 / raw.info["sfreq"]:0.6f}ms')
df.index = index[raw.first_samp:]

what I have in mind is that we can do

raw.to_data_frame(time_format='date')

to get the time as datetime64. Also I wonder why time is not set as index by default but It’s more a matter of taste

@hoechenberger @dengemann @drammock what do you think?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
drammockcommented, Jan 17, 2022

This is already supported. Quoting the docstring of the time_format:

If 'datetime', time values will be converted to :class:pandas.Timestamp values, relative to raw.info['meas_date'] and offset by raw.first_samp.

Setting time as index automatically is possible by passing index='time'. If I run your snippet up through raw.crop(tmax=10) and then:

In [5]: raw.to_data_frame(time_format='datetime', index='time')
Out[5]: 
channel                                MEG 0113   MEG 0112    MEG 0111  ...    EEG 059    EEG 060     EOG 061
time                                                                    ...                                  
2002-12-03 19:01:53.676070829+00:00   96.435548 -48.217774  101.074222  ...  38.854217  65.839113  285.661012
2002-12-03 19:01:53.677735789+00:00    0.000000 -28.930664   63.171389  ...  40.751037  68.002565  283.699953
2002-12-03 19:01:53.679400749+00:00    0.000000  -9.643555   75.805667  ...  40.995788  68.177980  280.431520
2002-12-03 19:01:53.681065709+00:00  125.366213  19.287110  101.074222  ...  41.179352  68.587282  279.124147
2002-12-03 19:01:53.682730669+00:00  163.940432   0.000000    0.000000  ...  39.343719  67.242433  281.738893
...                                         ...        ...         ...  ...        ...        ...         ...
2002-12-03 19:02:03.669161407+00:00  -19.287110 -38.574219 -176.879889  ...  44.299926  62.857057  265.396730
2002-12-03 19:02:03.670826367+00:00  -19.287110  -9.643555 -113.708500  ...  46.013183  64.552736  267.357790
2002-12-03 19:02:03.672491327+00:00  -28.930664   9.643555   25.268556  ...  50.418701  68.061036  273.240968
2002-12-03 19:02:03.674156288+00:00  -28.930664   9.643555   37.902833  ...  52.621460  69.405885  275.202028
2002-12-03 19:02:03.675821248+00:00  -77.148438  -9.643555  138.977056  ...  52.437896  69.522829  271.279909

[6007 rows x 376 columns]
0reactions
agramfortcommented, Jan 18, 2022

hum… I need to think… but i get your point.

Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Convert dataframe index to datetime
It should work as expected. Try to run the following example. import pandas as pd import io data = """value "2015-09-25 00:46" 71.925000...
Read more >
Pandas for time series data — tricks and tips - Adrian G
You need to have a datetime index on the df before running this. ... Group by column, apply operation then convert result to...
Read more >
pandas.to_datetime — pandas 1.5.2 documentation
Convert argument to datetime. This function converts a scalar, array-like, Series or DataFrame /dict-like to a pandas datetime object. ... Specify a date...
Read more >
Part 3 - Introduction to Pandas | ArcGIS API for Python
Let's convert the date columns to datetime64 type using pd.to_datetime() . We will dive into the details of individual date or time columns...
Read more >
Convert the column type from string to datetime format in ...
As we can see in the output, the data type of the 'Date' column is object i.e. string. Now we will convert it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found