Black Lives Matter. Please consider donating to Black Girls Code today.

Date Format does not work

I have imported a csv which was created by pandas dataframe.

The datetime column is formatted as such.
trend_df[“date_time”] = pd.to_datetime(trend_df[“date_time”], format=’%Y%m%d %H:%M:%S’)

But when I import it into Plotly, I cannot figure out how to format the date so it has the seconds.

How can I get the date formatted? I already googled this and followed instructions to go into Style-Axes-Format etc… No luck there either.

What I should be able to do is click on the dropdown arrow of date_time and just format it there like excel.

The dates imported come from this csv. Notice the format.

I’m having a little bit of trouble understanding the flow of what you’re doing here. The code you have above seems like it generates no separators between the year and month, the Chart Studio screenshot you have shows slashes as separators and the CSV screenshot you have shows dashes as separators. How are you importing into Chart Studio? Via the UI or via Python? Somewhere along the line it seems like your data got reformatted. The fact that the tab says “Sheet1” suggests that the CSV went through Excel or Google Sheets or something first, which may be the source of the reformatting.

In any case: Chart Studio has no formatting/reformatting options (or indeed any data-manipulation features) in the grid view, and only accepts datetime data in ISO format i.e. the format in your final screenshot. When you create a chart using data in this format, you can set the axis type to date and use the axis formatting directives to reformat e.g. the tick labels however you like.

Here is a screenshot of the file I am importing. You are correct, that it is a CSV I’m trying to import. By default, Windows opens it as an Excel file which gave it the name “Sheet 1”. So today I set it as a Notepad file, but it is still a CSV with the datetime formatted as shown. I’m simply dragging the file into the upload section of Plotly, and somehow it gets reformatted without the time information.

Hmm, this is pretty strange… I’m trying to replicate the behaviour you’re seeing but I’m unable to. I’ve got the following file and it’s importing as I would expect:

header 1,header 2
2020-01-01 00:00:00,string
2020-01-01 00:00:00,string

Here’s a new twist. I copied your csv data directly and pasted into a notepad. It works as expected. So I went and added my own data and the dates get re-formatted!!!
Lastly, I got rid of my floats and just put string in - same results. I believe I’ve reached rock bottom here.

header 1,header 2
2020-01-01 00:00:00,string
2020-01-01 00:00:00,string
2020-09-01 00:00:00,string
2020-09-01 00:01:00,string
2020-09-01 00:02:00,string



That’s quite strange indeed… I still can’t replicate this, unfortunately. Can you upload the CSV you’re using to some shared storage and share a link please? (the short one from your last screenshot, not your original one)

Here you go. Thanks, and good luck!

So I can import this file into and it loads as expected, without slashes. I really can’t imagine where these slashes are coming from!

I got it!

So I downloaded the csv from where I uploaded it (so to recreate your steps exactly) and it failed… Still reformatted the dates with slashes and no timestamp.

Then I decided to try this on my Mac. Repeated the steps and it works no problem! Screen Shot 2020-10-16 at 8.12.56 AM

Root cause: Somehow Windows is converting this csv during the upload process. I don’t know who’s bug this is (microsoft or plotly), but worth looking into. I can’t be the only one with this issue. It’s not like I had a weird date format or anything.

Hmm yeah. I still think there’s a spreadsheet somewhere in the mix here because your screenshot above says “newtest.csv.csv - Sheet1” which is what happens when our importer processes an Excel file. When you open with Notepad is it possibly saving through to a spreadsheet format? Your last screenshot has the tab called “newtest.csv” which is what I see when I do it (on my mac)

I’m using the same csv file that we’re sharing. Both on Mac and the PC. The file “newtest.csv.csv” is actually the same file as well, but I was just testing if it made a difference by explicitly naming a csv file “newtest.csv”.

Eventually I want to just put the data in by Python, but for now I’ll just do my testing on the Mac and avoid the headache.