将datetime64 [ns,UTC]隐式转换为datetime64 python

时间:2020-05-29 09:27:51

标签: python datetime

我有一列日期时间为UTC日期

2020-01-01 00:00:00+00:00   

有人可以帮我将其转换为datetime64,例如:

2020-01-01 

2 个答案:

答案 0 :(得分:1)

如果您使用熊猫(似乎是这种情况),则可以这样更改:

df.dates.apply(lambda x: x.date()) # where dates is your datetime column

答案 1 :(得分:0)

使用Python 3.2+尝试private void btnTestDate_Click(object sender, EventArgs e) { string sFile = "C:\\temp\\ExcelWithDate.xlsx"; if (File.Exists(sFile)) { File.Delete(sFile); } try { using (SpreadsheetDocument xl = SpreadsheetDocument.Create(sFile, SpreadsheetDocumentType.Workbook)) { WorkbookPart wbp = xl.AddWorkbookPart(); WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>(); Workbook wb = new Workbook(); FileVersion fv = new FileVersion(); fv.ApplicationName = "Microsoft Office Excel"; Worksheet ws = new Worksheet(); SheetData sd = new SheetData(); WorkbookStylesPart wbsp = wbp.AddNewPart<WorkbookStylesPart>(); wbsp.Stylesheet = CreateStylesheetForDate(); wbsp.Stylesheet.Save(); Columns columns = new Columns(); columns.Append(CreateColumnData(1, 1, 30)); columns.Append(CreateColumnData(2, 4, 23.5703125)); columns.Append(CreateColumnData(6, 6, 6.5703125)); ws.Append(columns); Row r; Cell c; // content r = new Row(); c = new Cell(); c.DataType = CellValues.String; c.CellValue = new CellValue("PROD12345"); r.Append(c); sd.Append(r); // date r = new Row(); c = new Cell(); c.DataType = CellValues.Date; c.StyleIndex = 1; c.CellValue = new CellValue(DateTime.Now.ToOADate().ToString(new CultureInfo("en-US"))); //c.CellValue = new CellValue(DateTime.Now.ToOADate().ToString()); //c.CellValue = new CellValue(DateTime.Now.ToOADate().ToString().Replace(".", ",")); r.Append(c); sd.Append(r); ws.Append(sd); wsp.Worksheet = ws; wsp.Worksheet.Save(); Sheets sheets = new Sheets(); Sheet sheet = new Sheet(); sheet.Name = "Sheet1"; sheet.SheetId = 1; sheet.Id = wbp.GetIdOfPart(wsp); sheets.Append(sheet); wb.Append(fv); wb.Append(sheets); xl.WorkbookPart.Workbook = wb; xl.WorkbookPart.Workbook.Save(); xl.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } } private static Stylesheet CreateStylesheetForDate() { Stylesheet ss = new Stylesheet(); Fonts fts = new Fonts(); DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font(); FontName ftn = new FontName(); ftn.Val = "Calibri"; FontSize ftsz = new FontSize(); ftsz.Val = 11; ft.FontName = ftn; ft.FontSize = ftsz; fts.Append(ft); fts.Count = (uint)fts.ChildElements.Count; Fills fills = new Fills(); Fill fill; PatternFill patternFill; fill = new Fill(); patternFill = new PatternFill(); patternFill.PatternType = PatternValues.None; fill.PatternFill = patternFill; fills.Append(fill); fills.Count = (uint)fills.ChildElements.Count; Borders borders = new Borders(); Border border = new Border(); border.LeftBorder = new LeftBorder(); border.RightBorder = new RightBorder(); border.TopBorder = new TopBorder(); border.BottomBorder = new BottomBorder(); border.DiagonalBorder = new DiagonalBorder(); borders.Append(border); borders.Count = (uint)borders.ChildElements.Count; CellStyleFormats csfs = new CellStyleFormats(); CellFormat cf = new CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; csfs.Append(cf); csfs.Count = (uint)csfs.ChildElements.Count; uint iExcelIndex = 164; NumberingFormats nfs = new NumberingFormats(); CellFormats cfs = new CellFormats(); cf = new CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cfs.Append(cf); NumberingFormat nf; nf = new NumberingFormat(); nf.NumberFormatId = iExcelIndex++; nf.FormatCode = "dd/mm/yyyy"; nfs.Append(nf); cf = new CellFormat(); cf.NumberFormatId = nf.NumberFormatId; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cfs.Append(cf); nfs.Count = (uint)nfs.ChildElements.Count; cfs.Count = (uint)cfs.ChildElements.Count; ss.Append(nfs); ss.Append(fts); ss.Append(fills); ss.Append(borders); ss.Append(csfs); ss.Append(cfs); CellStyles css = new CellStyles(); CellStyle cs = new CellStyle(); cs.Name = "Normal"; cs.FormatId = 0; cs.BuiltinId = 0; css.Append(cs); css.Count = (uint)css.ChildElements.Count; ss.Append(css); DifferentialFormats dfs = new DifferentialFormats(); dfs.Count = 0; ss.Append(dfs); TableStyles tss = new TableStyles(); tss.Count = 0; tss.DefaultTableStyle = "TableStyleMedium9"; tss.DefaultPivotStyle = "PivotStyleLight16"; ss.Append(tss); return ss; } 软件包。

datetime